Problem with multiplying doubles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christine

    Problem with multiplying doubles

    Hello,

    I wanted to create a currency translator with PHP.

    The code looks like this:
    <?php

    $faktor=2.95583 ; // a double
    $dm = $euro * $faktor; // $euro is a double variable with the
    original value
    // $dm is a double variable

    echo $dm; // prints 10 when $euro is 5

    ?>

    all varables are double, I checked this with the gettype() function
    The calculation seems to take only the integers and cuttig off the .9
    before calculating

    Another example:
    echo 5.5*2.2; // prints 10, should be about 11

    what can I do if I want to multily doubles to get the correct answer?

    I hope anyone can help me!
    thanks in advance for your help!

    christine
  • Jon Kraft

    #2
    Re: Problem with multiplying doubles

    christine@hutt-edv.de (christine) wrote:
    [color=blue]
    > $faktor=2.95583 ; // a double
    > $dm = $euro * $faktor; // $euro is a double variable with the
    > original value
    > // $dm is a double variable
    >
    > echo $dm; // prints 10 when $euro is 5[/color]

    No, it prints 14.77915 when $euro is 5
    [color=blue]
    > Another example:
    > echo 5.5*2.2; // prints 10, should be about 11[/color]

    No, it prints 12.1, and that's exactly what it should do.

    JOn

    Comment

    • Savut

      #3
      Re: Problem with multiplying doubles

      No it worked for me ...

      Savut

      "christine" <christine@hu tt-edv.de> wrote in message
      news:f17610db.0 311260240.6267a 4f8@posting.goo gle.com...[color=blue]
      > Hello,
      >
      > I wanted to create a currency translator with PHP.
      >
      > The code looks like this:
      > <?php
      >
      > $faktor=2.95583 ; // a double
      > $dm = $euro * $faktor; // $euro is a double variable with the
      > original value
      > // $dm is a double variable
      >
      > echo $dm; // prints 10 when $euro is 5
      >
      > ?>
      >
      > all varables are double, I checked this with the gettype() function
      > The calculation seems to take only the integers and cuttig off the .9
      > before calculating
      >
      > Another example:
      > echo 5.5*2.2; // prints 10, should be about 11
      >
      > what can I do if I want to multily doubles to get the correct answer?
      >
      > I hope anyone can help me!
      > thanks in advance for your help!
      >
      > christine[/color]


      Comment

      • Pedro Graca

        #4
        Re: Problem with multiplying doubles

        Jon Kraft wrote:[color=blue]
        > christine@hutt-edv.de (christine) wrote:
        >[color=green]
        >> $faktor=2.95583 ; // a double
        >> $dm = $euro * $faktor; // $euro is a double variable with the
        >> original value
        >> // $dm is a double variable
        >>
        >> echo $dm; // prints 10 when $euro is 5[/color]
        >
        > No, it prints 14.77915 when $euro is 5
        >[color=green]
        >> Another example:
        >> echo 5.5*2.2; // prints 10, should be about 11[/color]
        >
        > No, it prints 12.1, and that's exactly what it should do.
        >
        > JOn[/color]

        $ cat xx.php
        <?php
        ini_set('precis ion', '2');

        $euro = 5;
        $faktor = 2.95583;
        $dm = $euro * $faktor;

        echo "--------\n$dm\n";

        echo 5.5*2.2, "\n";
        ?>

        $ php xx.php
        --------
        15
        12



        Check your php.ini !
        --
        --= my mail address only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10K =--

        Comment

        • christine

          #5
          Re: Problem with multiplying doubles

          christine@hutt-edv.de (christine) wrote in message news:<f17610db. 0311260240.6267 a4f8@posting.go ogle.com>...[color=blue]
          > Hello,
          >
          > I wanted to create a currency translator with PHP.
          >
          > The code looks like this:
          > <?php
          >
          > $faktor=2.95583 ; // a double
          > $dm = $euro * $faktor; // $euro is a double variable with the
          > original value
          > // $dm is a double variable
          >
          > echo $dm; // prints 10 when $euro is 5
          >
          > ?>
          >
          > all varables are double, I checked this with the gettype() function
          > The calculation seems to take only the integers and cuttig off the .9
          > before calculating
          >
          > Another example:
          > echo 5.5*2.2; // prints 10, should be about 11
          >[/color]

          I installed PHP 4.3.4 instead of PHP 4.0.0 and now it works.

          Thanks for your answers!

          christine

          Comment

          Working...