-1

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???

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
uPrompt
  • 159
  • 1
  • 10

1 Answers1

1

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?".

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • I assumed that ini setting reflected the decimal precision of the number, not the whole number's precision. Apparently that means that if I have the precision at 14 and a number with 12 integer digits, I only get2 decimal points. That just makes no sense to me. – uPrompt Oct 29 '21 at 17:36
  • @uPrompt "Significant digits" is a fairly common concept, and not unique to PHP. In relative terms, 0.00000000000002 is twice 0.00000000000001, but 123456789012.002 is only a tiny fraction of a percent higher than 123456789012.001. The concept of floating point numbers uses a similar trade-off - the range of magnitudes which can be represented is greater, at the cost of precision. If you need high precision maths, you need to use a library such as GMP or BCMath. – IMSoP Oct 30 '21 at 09:21