Playing with some calculations and noticed something odd. Here's a dump of some calculated numbers stored in an object.
["julianTime"]=>
float(0.92518518518519)
["julian"]=>
float(2459516.4251852)
["j2000"]=>
float(2021.8245727178)
["b1950"]=>
float(2021.8247835323)
["j1900"]=>
float(2021.8245727178)
["delta"]=>
float(72.316207312938)
["terrestrial"]=>
float(2459516.4251852)
["universal"]=>
float(2459516.4243482)
It appears to be chopping off the decimal to fit a specific length. Calculating the same numbers on the same machine using JS, I get this:
"julianTime": 0.9251851851851852,
"julian": 2459516.4251851854,
"j2000": 2021.8245727178246,
"b1950": 2021.8247835323352,
"j1900": 2021.8245727178246,
"delta": 72.3162073129384,
"terrestrial": 2459516.4251851854,
"universal": 2459516.4243481923,
Now I know I've seen php and js do some really really strange things with precision and JSON. And Ive ini_set("precision", 14) and php is still chopping off the decimal places.
Is this because it's being stored in an object???
CodePudding user response:
There are two important phrases in your question:
Here's a dump of ...
What you are seeing is a string representation of numbers which are actually stored in a complicated binary format called "floating point".
The numbers shown are not what PHP is using internally to calculate further values.
I've used ini_set("precision", 14)
If you count the "significant digits" (after any leading zero) in the examples shown, you will see that they all have exactly 14, as you requested.
The manual page for that setting says:
The number of significant digits displayed in floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.
So if you try ini_set("precision", -1)
, you may see a different result. It won't change the actual values calculated, though, just the display.
The actual precision used in calculations is not configurable, it is part of PHP, and is not easy to understand without first understanding what "floating point" means. See the reference question titled "Is floating point math broken?".