Problem with parseFloat

I found a weird problem with parseFloat. This is what I tried to do
parseFloat("167.89")+ parseFloat("-67.89").It returned 99.9999988, but not 100.Why is this so?Is this a bug with parseFloat?Please giv me a solution for this?

Comments

  • Unfortunately there is no immediate solution to the float persision problem documented by PHP here: [link=http://www.php.net/manual/en/language.types.float.php]http://www.php.net/manual/en/language.types.float.php[/link]

    My suggestion is to round to whole number
    OR pattern match (with quantifiers) two simple cases something like this:
    /(9){x,}(/d)*/ is 9 repeated x or more times and match the remaining
    then using appropriate function remove above and round the rest [b]up[/b]
    OR
    /(0){x,}(/d)*/ is 0 repeated x or more times and match the remaining
    then using appropriate function remove above and round the rest [b]down[/b]

    [b]but[/b] make sure you're looking for numbers to the right of decimal places.

    Although javascript is powerful for processing data, there is very limited support for arithmetic (look into Math Object) which makes it faster without the overhead. Below is the code I used to discover the bug... I gave up because of this very bug, but you can use it to see the bug easily... it's not the parseInt function:

    [code]


    Calculator




    var displayIt =""; //keeps up with output string
    var firstNumber ="True"; //the first number entered is put directly into accumulator with no math performed
    var accumulator =0; //keeps tally on total
    var nextOperand =0; //holds operand until next operation
    var currentNumber =0; //keeps up with working number
    var numberLessThan1=1; //keeps up with working number decimal place
    var answer =0; //this will be used to display total when = is pressed



    /*********************************
    *
    * this function sets all variables
    * back to initial state
    *
    ***********************************/

    function ReInitialize()
    {
    displayIt =""; //keeps up with output string
    firstNumber ="True"; //the first number entered is put directly into accumulator with no math performed
    accumulator =0; //keeps tally on total
    nextOperand =0; //holds operand until next operation
    currentNumber =0; //keeps up with working number
    numberLessThan1=1; //keeps up with working number decimal place
    answer =0; //this will be used to display total when = is pressed
    DisplayPost(displayIt); //resets what is on the screen
    }



    /*********************************
    *
    * this function puts the input on
    * the little calculator screen
    *
    ***********************************/

    function DisplayPost(value) // the value is what is on the button
    {
    displayIt += value; // adds(concaterate) the string as buttons are pressed

    document.all.displayScreen.innerHTML = displayIt; // physically puts the string in the calculator display
    }



    /*********************************
    *
    * this function is only called
    * when a number button is pressed
    *
    ***********************************/

    function NumberResponse(number) // the number is a number from 0-9
    {
    if (numberLessThan1 == 1){
    currentNumber = currentNumber*10+number; // since only one number at a time is taken it is necessary
    // to use arithmatic to carry numbers as they are entered
    }else{
    currentNumber += number*numberLessThan1; // when working with decimals the number gets smaller
    Decimalz(); // in increments of 10 and that number is then put
    // in the next decimal slot

    } //end if
    DisplayPost(number); // send the number to DisplayPost function to be put on calculator screen

    }


    /*********************************
    *
    * this function is only called
    * when the decimal . is pressed
    *
    ***********************************/

    function Decimalz()
    {
    if (numberLessThan1 == 1){
    DisplayPost(".");
    }
    numberLessThan1 /= 10;
    }


    /*********************************
    *
    * this function is only called
    * when a operator is pressed
    *
    ***********************************/

    function Arithmatic(operand) // the oprand is passed when you choose +,-,x,ect.
    { // upper letter characters represent operands


    if (firstNumber == "True"){ // the first time a operand is passed
    accumulator = currentNumber; // put that number into accumulator x = currentNumber
    currentNumber = 0; // get ready for next number ?
    numberLessThan1 = 1 // getting ready next number
    firstNumber = "False"; // next time we will have 2nd number x + ? = ?
    }else{ // second time around we will have 2 numbers x + y = ?
    DoArithmatic(nextOperand); // send stored operand to do arithmatic function
    } //end if // at this point we should have both of the numbers
    // now we can perform an operation it will look like:

    // accumulator + currentNumber = Answer
    // depending on the operator + could be - or (* for multiply)

    // before getting to that it is important to note
    nextOperand = operand; // when an operand is passed it is not ready for execution
    // as the first number we have x + but nothing else
    // so the operand is stored for later.
    // this is where order of execution (step-by-step algorithms)
    // can be very tricky... if i put the nextOperand assignment
    // before the if-then-else then the DoArithmatic(nextOperand)
    // it would send the current operand so x + y - ? = ?
    // would instead have become x - y ???????
    // or if operand were the = it would x = y
    // that is why nextOperand holds the operand
    // first get x, store the operand, then get y, then use the operand
    // complicated it is, advanced coding
    // just remember that order is very important in
    // algorithm design


    // now a switch is used to display the button that was pressed
    switch (operand) // a switch structure is like if-elseif condition
    { // but it is easier to use than the if-then
    case 1: // if A is sent it means addition
    DisplayPost(" + "); // gotta send something to the calculator display
    break; // break signals the end of the If A
    case 2: // now we are on if B
    DisplayPost(" - "); // then it's subtraction
    break; // that's the end of that if
    case 3: // now C
    DisplayPost(" x "); // multiplication
    break; // gotta do the break
    case 4: // can't forget division
    DisplayPost("
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion