Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nanako · Apr 12, 2015 at 03:56 PM · mathfloat

How do i get the part of a float after the point?

What i want to do isn't too complex, i think. I want to create a weighted luck-based rounding algorithm. For example, feeding 1.5 into it, will have a 50% chance each way, of giving 1, or 2. Feeding 1.79 into it will have a 79% chance of returning 2, and a 21% chance of returning 1. In other words, the chance of rounding up is equal to how far it is towards the next integer.

The simplest way i can think of to do this would be to grab whatever is after the point, and generate a random number in the 0..1 range, round up if lower, round down if higher.

but how do i get that part? is there a better way than converting it to string and searching it?

is there a better way to achieve my goal that i'm missing?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by _Gkxd · Apr 12, 2015 at 06:34 PM

The modulus (remainder) operator (%) works with floats too.

So decimalPart = yourFloat % 1 would be what you need.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · Apr 12, 2015 at 06:39 PM 1
Share

Another option to go along with the $$anonymous$$odulus operator would be $$anonymous$$athf.Repeat.

 float myVar = 3.14159;
 myVar = $$anonymous$$athf.Repeat(myVar, 1.0f);
 Debug.Log(myVar); // 0.14159
avatar image Nanako · Apr 12, 2015 at 07:20 PM 0
Share

now this i like, its more compact and elegant than flooring or int casting.

Not that it matters, but is using % or repeat likely to be any faster than the other suggestions?

avatar image Eno-Khaon · Apr 12, 2015 at 07:41 PM 0
Share

$$anonymous$$odulus is made to be fast, but it's not naturally supported by float values. Therefore, workarounds have been made to support the same functionality as well as possible, but they still won't be as fast as modulus operations on non-float values. $$anonymous$$athf.Repeat is probably the "most appropriate" way of doing this, but the easiest way to be sure what's fastest is to just create a loop and calculate something a few (hundred?) million times to see what's faster or slower to calculate.

avatar image _Gkxd · Apr 12, 2015 at 08:13 PM 0
Share

Talking about how the speed of these types of operations is a bit of a moot point. You won't actually be able to tell the difference, since they are all sufficiently cheap, so it's essentially up to whatever you think looks better in code.

It's much more likely that other areas in a game are bottlenecks (e.g. searching through large lists, high-polygon mesh collisions, lighting calculations over large scenes, large image files, etc.), and identifying and making those bottlenecks faster is much more important than optimizing something like this. After all, if a high-poly mesh (or something else) is causing the lag, switching from modulus to some other operation won't fix the lag.

avatar image
2

Answer by Eric5h5 · Apr 12, 2015 at 04:36 PM

Definitely don't use strings. You can just subtract the integer part.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Taziar · Dec 27, 2017 at 01:02 AM

I am assuming you are converting from Float to Int?

Value += Random.value; int value = (int) Value;

Random.Value will add a random value 0 - 1.0 to the number, possibly increasing it to the next integer. The higher the decimal starts, the more likely that is. Casting it will then strip the decimal. (or you could also use Mathf.Floor if you want to keep it a Float)

(if you want negative numbers to round away from zero...)

if (value >0) Value += Random.value; if (value <0) Value -= Random.value; int rounded = (int) value;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Jan 18, 2018 at 02:28 PM 0
Share

Yours is the correct answer.
Example: Value = 1.5; result is 1 as long as Random.value < 0.5, then it is 2 (1 - 0.5 => p = 0.5) Value = 1.79; result is 1 as long as Random.value < 0.21, then it is 2 (1 - 0.21 => p = 0.79)

avatar image
0

Answer by GraySky · Apr 12, 2015 at 05:52 PM

The following should do what you want. As Eric said, no need to use strings.

     public float GetMyRandom (float number)
     {
         float floored = Mathf.Floor (number);
         float fractionalPart = number - floored;
         
         float randNum = Random.Range (0, 1f);
 
         if (randNum >= fractionalPart) {
             return Mathf.Ceil (number);
         } else {
             return floored;
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Gimos · Apr 12, 2015 at 05:53 PM

 private int GodWill (float number) {
     int p = (int)((number - (int)number) * 100);
     
     if (p < (int)Random.Range (0, 100))
         return (int)number + 1;
     else 
         return (int)number;
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Float to Int 4 Answers

Maths issue + Null reference error? -1 Answers

Why can't I get a floating point value here?... 1 Answer

[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers

How do I add something on to a float. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges