Jump to content

Why data type REAL returns almost-correct values for decimals?


benphp

Recommended Posts

MS SQL Server - data type "real"

 

I add a value of 95.4, and that's what displays in the SQL table, but when I fetch the value via PHP, I get 98.400002. Other values:

 

98.2 = 98.199997

98.3 = 98.300003

98.4 = 98.400002

98.5 = 98.5

98.6 = 98.599998

98.7 = 98.699997

98.8 = 98.800003

98.9 = 98.900002

 

WTF?

 

I can use round to fix it, but would rather fix the problem than the symptom.

 

Help?

 

Thanks!

Link to post
Share on other sites

But when I input exactly 98.8, it displays 98.8 in SQL Server Management Studio, but when I use PHP, it returns 98.800003. Why is it one value in one place and something else in another?

 

It's not my database, or I'd change it.

 

Link to post
Share on other sites

Why is it one value in one place and something else in another?

 

Not all software treats floats the same way.  Some software may use more or less bits to represent the value, or may have different levels of precision/rounding.  There's a whole slew of problems/quirks one has to deal with when working with floating point values.  That is why it is generally recommended you avoid them when possible.

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.