This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

[Bug] Floats calculations


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
disawar1
AlliedModders Donor
Join Date: Aug 2011
Location: Russian
Old 01-03-2013 , 05:58   [Bug] Floats calculations
Reply With Quote #1

I always use debug logs when writing plugins. Today I opened the log file and saw 1.2 instead of 1.3 (%.1f). So I spent a lot of time, to find which part of the code should be fixed. I think everything is fine with my code and it's format specifiers bug. But I have my doubts..maybe it's specificity of pawn language? I mean rounds a float to the next lowest value after division. What really is equal fModifier variable? Thanks.

PHP Code:
    decl String:sBuffer[24];
    
    new 
Float:fModifier 1560.0 1200.0;        // 1.3
    
FloatToString(fModifiersBuffer24);
    
    
PrintToServer("%f %s"fModifiersBuffer); 
Result:
Code:
1.299999 1.300000
__________________

Last edited by disawar1; 01-03-2013 at 09:42.
disawar1 is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 01-03-2013 , 06:29   Re: [Bug] Floats calculations
Reply With Quote #2

Maybe you can get more infos in the comments of this bug.

Yours sincerely
Impact
__________________
Impact123 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-03-2013 , 09:42   Re: [Bug] Floats calculations
Reply With Quote #3

Welcome to the fun of floating point math.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Sheepdude
SourceMod Donor
Join Date: Aug 2012
Location: Chicago
Old 01-03-2013 , 09:49   Re: [Bug] Floats calculations
Reply With Quote #4

http://en.wikipedia.org/wiki/IEEE_floating_point

Knock yourself out.

As a side note, if you use Format(sBuffer, 24, "%f", fModifier) instead of FloatToString, the values will both be 1.299999. So FloatToString must do some rounding at some point.
__________________

Last edited by Sheepdude; 01-03-2013 at 09:51.
Sheepdude 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 20:45.


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