AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   #define val 1.05 is actually 1.049999 ? (https://forums.alliedmods.net/showthread.php?t=47051)

Geesu 11-08-2006 15:51

#define val 1.05 is actually 1.049999 ?
 
So basically I have this:

Code:
#define ITEM_BOOT_INCREASE    1.05                    // Represents % increase

And I do a little math later:

( ( ITEM_BOOT_INCREASE - 1.00 ) * 100.00 )

Which (in theory) is 5.

But when doing client_prints and checking the results:

Code:
client_print( id, print_chat, "(%f - %f) * %f = %0.0f", ITEM_BOOT_INCREASE, 1.00, 100.00, ( ( ITEM_BOOT_INCREASE - 1.00 ) * 100.00 ) );

This presents the output:
Code:
(1.049999 - 1.000000) * 100.000000 = 4

Why is 1.05 represented as 1.049999 ? And how do I get it to be 1.05?

Thanks,
Josh

schnitzelmaker 11-08-2006 19:06

Re: #define val 1.05 is actually 1.049999 ?
 
floats are not very excact, some floats doesnt exist.
???,here the explain:floats on computer are based on hex.
1.05 are in hex :3F866666,but 3F866666 are also 1.049999952,so some floats doenst exist or exist twice.
This problem is based on the mathematic of floats.

To get 1.05 you must use 1.051 , 1.052


All times are GMT -4. The time now is 20:17.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.