AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Float number calculations (https://forums.alliedmods.net/showthread.php?t=221155)

dFF 07-18-2013 15:39

Float number calculations
 
For example i have:

PHP Code:

static Float:fStartFloat:fAdd
    
fStart 
getDayValue)
fAdd fStart 0.1

client_print
idprint_chat"fStart: %f"fStart )
client_printidprint_chat"fAdd: %f"fAdd )

Float:get_DayValueday )
{
    if( 
day == )
        return 
80.0
    
    
return 1.0



debug say:

Code:

fStart: 80.000000
fAdd: 80.099998

why not increased with 8.1 ? I want everytime 80.0 increased like: 80.1, 80.2, 80.3, 80.4 ..

ConnorMcLeod 07-18-2013 15:43

Re: Float number calculations
 
Dunno if it is the way it is printed, or if it is due to float precision, anyway, internal value should be ok.

dFF 07-18-2013 15:45

Re: Float number calculations
 
Yeah .. the printed is same, floats are strange in amxx, and i need to use floats.

zeus 07-18-2013 16:10

Re: Float number calculations
 
I have made a similar test

Code:

new Float:a = 32.0;
new Float:b = 0.13;
new Float:s;
       
s = a + b;
client_print(id,print_chat,"%f",s);

The result shown is

Code:

32.130001
Tested with latest HLDS build, AMXX Build ID: 1.8.3-dev 140:f57bd4cee0b7, runing CentOS 6 64bit.

ConnorMcLeod 07-18-2013 20:39

Re: Float number calculations
 
This is how float works in pawn or in c++, so what i said that should be ok internally seems wrong.

You can read those interesting thread :
https://forums.alliedmods.net/showthread.php?t=53543
https://forums.alliedmods.net/showthread.php?t=53535
https://forums.alliedmods.net/showthread.php?t=47051

Anyway, in your case, multiply all by 10 and you can works with integers.

And if you want to print as Float, you can trick it like this :

PHP Code:

    static iNumber 800// instead of 80.0

    
new szNumber[32];
    new 
len formatex(szNumbercm(szNumber), "%d", ++iNumber); // add 1 instead of 0.1
    
szNumber[len] = szNumber[len-1];
    
szNumber[len-1] = '.';
    
client_print(idprint_chatszNumber); 


.Dare Devil. 07-18-2013 21:52

Re: Float number calculations
 
somehow, sometimes the floats act this way...
Nothing wrong with you code and there is nothing ( i think ) you can do.
Also they work in c++ same way.

dFF 07-19-2013 13:54

Re: Float number calculations
 
Thank you again Connor :wink:.


All times are GMT -4. The time now is 06:05.

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