PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Why is serialize() turning my round()'d numbers back into loooong floats!?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Probably because of the format of float - I think you can't have an exact value of 349720.02 - floats are always presented with some precision error.
You need a fixed point format, IMHO. That is - serialize the float as integer multiplied by 100, and when you unserialize - divide by 100. So you have a fixed point format - Y.XX
Last edited by VladSun on Sun Feb 01, 2009 5:11 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
For a lot of numbers the floating-point format does not precisely describe the number. It gets close but not exact.
0.02 is 1/50 is 1/64 + 1/256 + 1/1024 + 1/4096 + 1/4194304 +... Each denominator is a power of two, and that restriction means the series won't end. No exact representation.
There's a php.ini setting. Not quite sure how it's used because it doesn't seem to work as I expect.
; When floats & doubles are serialized store serialize_precision significant
; digits after the floating point. The default value ensures that when floats
; are decoded with unserialize, the data will remain the same.
serialize_precision = 100
Anyways, that's the problem: PHP is storing all the data it has so that the unserialized number is as "accurate" as possible.
This particular feature is a bit inconvenient. I thought rounding two two places (even if it was a float) would be two places, period. I guess I can understand it though.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.