Raised This Month: $51 Target: $400
 12% 

Float number calculations


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 07-18-2013 , 15:39   Float number calculations
Reply With Quote #1

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 ..

Last edited by dFF; 07-18-2013 at 15:39.
dFF is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-18-2013 , 15:43   Re: Float number calculations
Reply With Quote #2

Dunno if it is the way it is printed, or if it is due to float precision, anyway, internal value should be ok.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 07-18-2013 , 15:45   Re: Float number calculations
Reply With Quote #3

Yeah .. the printed is same, floats are strange in amxx, and i need to use floats.
dFF is offline
zeus
Senior Member
Join Date: Jul 2008
Old 07-18-2013 , 16:10   Re: Float number calculations
Reply With Quote #4

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.
zeus is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-18-2013 , 20:39   Re: Float number calculations
Reply With Quote #5

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); 
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 07-18-2013 at 20:49.
ConnorMcLeod is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 07-18-2013 , 21:52   Re: Float number calculations
Reply With Quote #6

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.
.Dare Devil. is offline
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 07-19-2013 , 13:54   Re: Float number calculations
Reply With Quote #7

Thank you again Connor .
dFF is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:53.


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