php-src icon indicating copy to clipboard operation
php-src copied to clipboard

EXP(N) != M_E ** N

Open REGZ-PL opened this issue 4 years ago • 9 comments

Description

The following code:

<?PHP

	FUNCTION _EXP($N) { RETURN M_E ** $N; }

	ECHO ' EXP(1) = '. EXP(1) . '<br>';
	ECHO '_EXP(1) = '. _EXP(1) . '<br><br>';

	ECHO '(EXP(1) == _EXP(1)) = ' . (EXP(1) == _EXP(1) ? 'TRUE' : 'FALSE') . '<br><br>';

	ECHO ' EXP(2) = '. EXP(2) . '<br>';
	ECHO '_EXP(2) = '. _EXP(2) . '<br><br>';

	ECHO '(EXP(2) == _EXP(2)) = ' . (EXP(2) == _EXP(2) ? 'TRUE' : 'FALSE') . '<br><br>';

	ECHO ' EXP(3) = '. EXP(3) . '<br>';
	ECHO '_EXP(3) = '. _EXP(3) . '<br><br>';

	ECHO '(EXP(3) == _EXP(3)) = ' . (EXP(3) == _EXP(3) ? 'TRUE' : 'FALSE') . '<br><br>';

	ECHO ' EXP(4) = '. EXP(4) . '<br>';
	ECHO '_EXP(4) = '. _EXP(4) . '<br><br>';

	ECHO '(EXP(4) == _EXP(4)) = ' . (EXP(4) == _EXP(4) ? 'TRUE' : 'FALSE') . '<br><br>';

Resulted in this output:

EXP(1) = 2.718281828459
_EXP(1) = 2.718281828459

(EXP(1) == _EXP(1)) = TRUE

EXP(2) = 7.3890560989307
_EXP(2) = 7.3890560989306

(EXP(2) == _EXP(2)) = FALSE

EXP(3) = 20.085536923188
_EXP(3) = 20.085536923188

(EXP(3) == _EXP(3)) = FALSE

EXP(4) = 54.598150033144
_EXP(4) = 54.598150033144

(EXP(4) == _EXP(4)) = FALSE

But I expected this output instead:

EXP(1) = 2.718281828459
_EXP(1) = 2.718281828459

(EXP(1) == _EXP(1)) = TRUE

EXP(2) = 7.3890560989307
_EXP(2) = 7.3890560989307

(EXP(2) == _EXP(2)) = TRUE

EXP(3) = 20.085536923188
_EXP(3) = 20.085536923188

(EXP(3) == _EXP(3)) = TRUE

EXP(4) = 54.598150033144
_EXP(4) = 54.598150033144

(EXP(4) == _EXP(4)) = TRUE

PHP Version

PHP 8

Operating System

All

REGZ-PL avatar Apr 07 '22 10:04 REGZ-PL

Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE 754 is, read this: http://www.floating-point-gui.de/

Thank you for your interest in PHP.

cmb69 avatar Apr 07 '22 10:04 cmb69

To clarify, here, exp() is using a higher precision math internally, than you get using a power of M_E directly.

bwoebi avatar Apr 07 '22 10:04 bwoebi

@cmb69 - Assuming that Floating-Point precision is limited. I see EXP(3) = 20.085536923188, _EXP(3) = 20.085536923188 that is equal.

REGZ-PL avatar Apr 07 '22 10:04 REGZ-PL

@REGZ-PL, that is exactly the problem: the string representation of floating point numbers is not necessarily exact.

cmb69 avatar Apr 07 '22 11:04 cmb69

The problem occurs only in the range of 2 - 709 per 100,000 attempts.

(EXP(1) == _EXP(1)) = TRUE
(EXP(2) == _EXP(2)) = FALSE
...
(EXP(709) == _EXP(709)) = FALSE
(EXP(710) == _EXP(710)) = TRUE
...
(EXP(100000) == _EXP(100000)) = TRUE

REGZ-PL avatar Apr 07 '22 11:04 REGZ-PL

No. This is a general issue regarding floating point numbers; e.g. https://3v4l.org/qRqiP. You really should read and understand http://www.floating-point-gui.de/.

cmb69 avatar Apr 07 '22 11:04 cmb69

Main problems

  • Incorrect previous assumptions

    We are in 2022 and new opportunities allow us to solve the problem of incorrect assumptions of the precursors. Why adding the numbers 0.1 and 0.2 has to give 0.30000000000000004? The child would show that there is a mistake here. I understand that this is due to the way of storing data in the device's memory. I created an example of solving this problem in the modern world where 0.1 + 0.2 = 0.3 not 0.30000000000000004 and 2.71828182845904523536028747135266249775724709369995 + 2.71828182845904523536028747135266249775724709369995 = 5.43656365691809047072057494270532499551449418739990 is calculated without any problem. Excuse me for a bad programming style. @cmb69 - https://3v4l.org/2eENj

  • Calculations are not standardized

    Correct me if I am wrong, but for these functions the results should be identical and comparable.

    Code:

    <?PHP
    
    FUNCTION EXP1(INT $N) : float { RETURN M_E ** $N; }
    FUNCTION EXP2(INT $N) : float { RETURN pow(M_E, $N); }
    FUNCTION EXP3(INT $N) : float { RETURN bcpow(M_E, $N, 16); }
    
    var_dump(EXP(2));
    var_dump(EXP1(2));
    var_dump(EXP2(2));
    var_dump(EXP3(2));
    

    Output:

    Output for 8.0.1 - 8.0.17, 8.1.0 - 8.1.4
        float(7.38905609893065)
        float(7.3890560989306495)
        float(7.3890560989306495)
        float(7.389056098930404)
    Output for 7.4.0 - 7.4.28
        float(7.3890560989307)
        float(7.3890560989306)
        float(7.3890560989306)
        float(7.3890560989304)
    

REGZ-PL avatar Apr 07 '22 19:04 REGZ-PL

PHP is not responsible for the architecture of computer systems. If you have concrete suggestions for how PHP can fix floating-point arithmetic then consider submitting your ideas to an academic journal so that folks like IEEE as well as Intel and AMD can review your work.

In the meantime, round() your math.

damianwadley avatar Apr 07 '22 19:04 damianwadley

Why adding the numbers 0.1 and 0.2 has to give 0.30000000000000004?

Because it is fast, clock-cycle-level fast. In most cases exact values are not important anyway. However if you need the exact values e.g. for account balance, you should store values as cents using int. Other cases will do with rounding or BC Math extension.

KapitanOczywisty avatar Apr 07 '22 23:04 KapitanOczywisty