Raised This Month: $32 Target: $400
 8% 

Wrong Float Format


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
recar
Junior Member
Join Date: Feb 2018
Old 03-20-2018 , 13:13   Wrong Float Format
Reply With Quote #1

char text[256];

float point = 50.30;

Format( text , sizeof( text ) , "%.2f" , point );

Output: 50.29

why is it wrong?
recar is offline
Wyon
Junior Member
Join Date: Mar 2018
Old 03-20-2018 , 13:22   Re: Wrong Float Format
Reply With Quote #2

Rounding error... You probably can "hide" that by adding 0.000001 to float number
Wyon is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-20-2018 , 19:17   Re: Wrong Float Format
Reply With Quote #3

You can't exactly represent 5.3 in a 32-bit float. The closest choice, when truncated to two decimal places, is 50.29.
Fyren is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 03-21-2018 , 00:22   Re: Wrong Float Format
Reply With Quote #4

https://www.h-schmidt.net/FloatConverter/IEEE754.html

Useful for seeing how floats are represented internally and why you can't have exactly 50.30
hmmmmm is offline
recar
Junior Member
Join Date: Feb 2018
Old 03-21-2018 , 02:29   Re: Wrong Float Format
Reply With Quote #5

Quote:
Originally Posted by Fyren View Post
You can't exactly represent 5.3 in a 32-bit float. The closest choice, when truncated to two decimal places, is 50.29.
Quote:
Originally Posted by hmmmmm View Post
https://www.h-schmidt.net/FloatConverter/IEEE754.html

Useful for seeing how floats are represented internally and why you can't have exactly 50.30
Quote:
Originally Posted by Wyon View Post
Rounding error... You probably can "hide" that by adding 0.000001 to float number

how can I do 5.30 ?
recar is offline
Papero
Veteran Member
Join Date: Aug 2016
Location: Italy
Old 03-21-2018 , 08:35   Re: Wrong Float Format
Reply With Quote #6

Quote:
Originally Posted by recar View Post
how can I do 5.30 ?
5.301 ? rounding at 2 floats
__________________
My Plugins
SPCode


Steam: hexer504
Telegram: Hexah
Discord: Hexah#6903

If you like my work you can donate here!

Last edited by Papero; 03-21-2018 at 08:36.
Papero is offline
Reply


Thread Tools
Display Modes

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 11:07.


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