Mathematical differences?!

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Mathematical differences?!


    Hello all,

    I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
    different result than JS. For example:

    ====================
    t = 0.6255264658909423
    f = 20.5
    ln = -6.983333333333333
    d2r = 0.017453292519943295

    $ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
    360) * $d2r;

    PHP outputs 4.4156830075456535
    JS outputs 4.42304792511156
    ====================

    I found out that ((...) % 360) returns in PHP 253.0000... whereas JS and a
    Calculator return 253.6085....
    I also tried to use the bc...-functions but the result was the same.

    This formular is only a part of a bigger calculation and I also noticed
    further differences because when
    I manually set $ra to 4.4230... (the JS result) then I still get a different
    end-result than in JS but it is
    100% EXACTLY the same algorithm, I checked it many times.

    How could that be?! 1:1 the same code but different outputs? Is this a
    PHP-bug? :-?

    Kindly regards,

    Mario
    Mario Werner Guest

  2. Similar Questions and Discussions

    1. Anyone mathematical?
      Are you any good at maths? If so I was wondering if you can come up with a nice formular that I can program in Lingo that will calculate the angle...
    2. cfexecute for mathematical algorithms
      Hi I need to write C++ programs to do long mathematical algorithms which I want to combine into cfm pages. I know java is quicker but i dont use...
    3. [PHP] Mathematical differences?!
      How could that be?! 1:1 the same code but different outputs? Is this a PHP-bug? :-? *feelin' kinda' smart arsed today, if no one noticed* ...
    4. [PHP] Help with Mod mathematical function
      http://us4.php.net/math The operator you want is % Eg: 5%3 = 2 <desa15@necso.es> wrote in message...
    5. Help with Mod mathematical function
      Hi, I am porting a vb application across to php but have come up against a small problem. The code uses the Mod calculation however I cannot find...
  3. #2

    Default Re: [PHP] Mathematical differences?!

    * Thus wrote Mario Werner (mario@open-ware.de):
    >
    > I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
    > different result than JS. For example:
    >
    > ====================
    > t = 0.6255264658909423
    > f = 20.5
    > ln = -6.983333333333333
    > d2r = 0.017453292519943295
    >
    > $ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
    > 360) * $d2r;
    >
    > PHP outputs 4.4156830075456535
    > JS outputs 4.42304792511156
    > ====================
    >
    > I found out that ((...) % 360) returns in PHP 253.0000... whereas JS and a
    > Calculator return 253.6085....
    > I also tried to use the bc...-functions but the result was the same.
    > [...]
    > How could that be?! 1:1 the same code but different outputs? Is this a
    > PHP-bug? :-?
    Nope.

    [url]http://bugs.php.net/bug.php?id=12623[/url]

    fmod() will do what you want.

    Curt
    --
    "I used to think I was indecisive, but now I'm not so sure."
    Curt Zirzow Guest

  4. #3

    Default Re: [PHP] Mathematical differences?!

    Without spending a lot of time looking at your problem I would speculate
    this is the result of round off error.

    What is roundoff error you ask?

    Well the number 0.6255264658909423 does not designate
    0.6255264658909423000000000000... but 0.6255264658909423 and some
    fraction that could not be displayed because it is too large to be
    stored in the number of bits used.

    But the number of bits used per data type varies from system to system.

    So you are seeing round off error.

    If you don't understand e-mail me for a more complete explanation.

    -Dan

    On Wed, 2003-09-17 at 15:54, Mario Werner wrote:
    > Hello all,
    >
    > I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
    > different result than JS. For example:
    >
    > ====================
    > t = 0.6255264658909423
    > f = 20.5
    > ln = -6.983333333333333
    > d2r = 0.017453292519943295
    >
    > $ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
    > 360) * $d2r;
    >
    > PHP outputs 4.4156830075456535
    > JS outputs 4.42304792511156
    > ====================
    >
    > I found out that ((...) % 360) returns in PHP 253.0000... whereas JS and a
    > Calculator return 253.6085....
    > I also tried to use the bc...-functions but the result was the same.
    >
    > This formular is only a part of a bigger calculation and I also noticed
    > further differences because when
    > I manually set $ra to 4.4230... (the JS result) then I still get a different
    > end-result than in JS but it is
    > 100% EXACTLY the same algorithm, I checked it many times.
    >
    > How could that be?! 1:1 the same code but different outputs? Is this a
    > PHP-bug? :-?
    >
    > Kindly regards,
    >
    > Mario
    Dan Anderson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139