• 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 sdgd · Mar 19, 2013 at 07:49 AM · c#calculation

wired calculation I don't get it 30-30 = 1.907349E-06

IF I use this:

 float TempPCF = CreateFloor.PerCreateFake*100f;
 float TempPSF = CreateFloor.PerStopFake*100f;
 int TempPCFi = (int)TempPCF;
 int TempPSFi = (int)TempPSF;
 Debug.Log(""+TempPCFi);
 Debug.Log(""+TempPSFi);
 Debug.Log(""+TempPCF + " " + TempPSF);

I get resault

 50
 30
 50 30

I like this resault

BUT arghhh with this:

 float TempPCF = CreateFloor.PerCreateFake*100f;
 float TempPSF = CreateFloor.PerStopFake*100f;
 int TempPCFi = (int)TempPCF;
 int TempPSFi = (int)TempPSF;
     TempPCF -= TempPCFi;
     TempPSF -= TempPSFi;
 Debug.Log(""+TempPCFi);
 Debug.Log(""+TempPSFi);
 Debug.Log(""+TempPCF + " " + TempPSF);

I get resault:

 50
 30
 0 1.907349E-06

ARGHHH HOW DA can he calculate 30-30 = 1.907349E-06

and I don't belive 2 static have any kind of problems here as I've assign 2 temporary variables just for them

Comment
Add comment · Show 4
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 sparkzbarca · Mar 19, 2013 at 08:01 AM 0
Share

just so i follow this.

its not 30- 30

its a float thats approx thirty but is in fact not thirty forced into a int which makes it 30

but then you take that 30 and subtract this complex float

there is no reason to believe that float is 30. I see no debug that says it is. I see a debug that says when you force convert it to an int it becomes 30 but i don't see a debug log that shows the value of that float that your subtracting 30 from

if that float is 31.907349e-06 than your number makes perfect sense

I see no unanticipated behavior here

just weird force a float to be an int and then subtract it from itself what on earth are you trying to do?

avatar image Chronos-L · Mar 19, 2013 at 08:03 AM 0
Share

@sparkzbarca, 31.907349e-06 is not 30.000... , it is 0.0000319...

avatar image sparkzbarca · Mar 19, 2013 at 08:08 AM 1
Share

yea my bad i got the answer write though. :P

avatar image Fattie · Mar 19, 2013 at 08:12 AM 1
Share

it's this simple ... the OP does not realise that floats, on computers, are, well, floats... they are not exact.

sdgd, it's that simple O$$anonymous$$? Quite simply, floats on computers are NOT exact. (Of course not because they are ... floats! heh)

Normally computer code is peppered with 100s of lines of code to deal with this. Simple examples are you would rarely say "if equals zero" but rather "if less than or equal to zero", and you constantly have to use $$anonymous$$athf.Approximately, and you very frequently round numbers or floor numbers, in computer code.

Its just an absolutely normal, central part of computing.

Floats ... real numbers if you will .., are not "exact", they are floats.

2 Replies

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

Answer by Chronos-L · Mar 19, 2013 at 08:02 AM

1.907349E-06 is equal to 1.907349 x 10 ^ -6, which is equivalent to 0.000001907349 and very close to 0. It is caused by floating point imprecision. Unity even has a Mathf.Approximately to deal with checking the equality between 2 float.

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 sdgd · Mar 19, 2013 at 08:08 AM 0
Share

hey where did 1 answer go I have puted both thumbs up as they were both pretty much same

and thanks I thought it's 1.9 not 0.000019

it makes me breath more easily as now I can check if it's 0.1 bigger not 0

avatar image
1

Answer by xt-xylophone · Mar 19, 2013 at 08:03 AM

I think sometimes floats that say they are whole numbers are actually really close and not exact. It's kinda like a rounding error. In your case it's an error of only 0.000001907349

Casting won't help with this. Maybe try a rounding function before you do the maths?

Or if on the future you want to check if 2 floats are equal, try checking if the difference is less than a reeeaally small number instead of ==

Comment
Add comment · Show 2 · 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 Fattie · Mar 19, 2013 at 08:08 AM 2
Share

$$anonymous$$athf.Approximately exists for that purpose.

avatar image xt-xylophone · Mar 19, 2013 at 09:41 AM 0
Share

^ awesome, didn't know that one!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Instantiated GuiTextures not showing 1 Answer

Multiplayer Inventory System with RPCs 0 Answers

how to see class driven out of class in inspector? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges