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
0
Question by Persona · May 01, 2011 at 04:46 AM · errorsolutionfloatingpoint

Floating point correction

I'm making a rotation script for a slot machine and it works well with few bugs: a Floating point error that causes it to avoid landing squarely on a number (landing on 59.99997 instead of 60.0).

I need a way to remedy this and I've come up with two solutions:

1.) Compare two numbers to see if they equal each other, plus or minus 0.50. The current script compares the transform.rotation.

2.) Use Quaternion Slerp on a specific EulerAngle(EulerAngle.x alone to be exact) I heard that rotation done by Quaternion Slerp could stop floating point from appearing.

I know how to compare two numbers but not with an additional range of 0.50.

Can someone show me how to do either one or present some other solution?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by rejj · May 01, 2011 at 07:01 AM

If you know the number you need to compare against (in your example's case, 60) you can use Mathf.Approximately()

eg,

if (Mathf.Approximately(transform.rotation.x, 60f)) {
    // close enough, treat it like it was 60
}

edit

A function such as the following should help you with point 1 in your question:

bool AlmostEqual(float a, float b, float epsilon) {
    return Mathf.Abs(a - b) < epsilon;
}

In your example where you wanted to check if two numbers are within 0.5, you would pass in 0.5 as the epsilon value.

Comment
Add comment · Show 3 · 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 Persona · May 01, 2011 at 08:30 PM 0
Share

Since what it stops on is random, I need to compare it against the final rotation ins$$anonymous$$d of a predeter$$anonymous$$ed number

avatar image jamie 1 · May 03, 2011 at 07:08 PM 0
Share

What is your purpose? Is the thing stopping by itself, and you want to know where it stopped, or is it rotating and you want to check if its time to stop it?

avatar image Persona · May 05, 2011 at 03:05 PM 0
Share

It stops itself after a few seconds but once it does, it moves to a position in order to land on symbol.

avatar image
2

Answer by Meltdown · May 03, 2011 at 07:09 PM

The following code works for me. Simply convert your float to an int.

int xAngleInt = 0; float xAngle = 0;

void Update() { xAngle = 59.9994F; // Set your random angle here xAngleInt = Convert.ToInt32(xAngle); }

void OnGUI() { GUI.Label(new Rect(10, 10, 100, 100), xAngleInt.ToString()); }

In this case the value of '60' will always be shown. So no matter what xAngle is set to in your Update() method, your true integer value will only be shown.

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 Bravini · May 04, 2011 at 12:33 AM 0
Share

if you don't need the decimal value this is the best way to go I$$anonymous$$HO, if you do, just multiply by 10, 100, etc $$anonymous$$athf.Convert or $$anonymous$$atf.RoundToInt then divide back by the value used

avatar image
0

Answer by Joshua · May 01, 2011 at 05:17 AM

Don't use transform.eulerAngle, it won't work if the rotation is more then 360 degrees and if you're making a slot machine I imagine you'll want it to spin more then that. Use transform.Rotate instead. But in this case I would just use

Mathf.RoundToInt(59.99997) //returns 60

It will return the nearest int. If it's exactly in between two ints (like 1.5 for instance) it will return the even one of the two, so.. two in this case ^.^

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 Persona · May 01, 2011 at 05:41 AM 0
Share

It has to be Euler Angles with how the script is set up and even when I converted the script to work with rotation in place of Eulers, It didn't solve the problem

avatar image
0

Answer by almo · May 03, 2011 at 07:18 PM

You could keep the angle from last frame, and compare the two. If that difference is small (you pick how small) you decide that it's done moving, then set it equal to the nearest integer and set your display accordingly.

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

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

No one has followed this question yet.

Related Questions

Float check not returning the right value. 1 Answer

[solution] Error building Player: CommandInvokationFailure: Unable to kill the adb server. Please make sure the Android SDK is installed and is properly configured in the Editor 2 Answers

How can I create a working and semi-realistic lift force? 0 Answers

Unknown error 1 Answer

floating point model using by unity 0 Answers


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