What is this strange error "7.1054273576E-15"?

Discussion in 'PHP' started by greatlogix, Feb 19, 2009.

  1. #1
    I am doing simple minus from total in a loop if in a loop both variable values are same (i.e 27.99 - 27.99) I am getting "7.1054273576E-15" as a result.

    I don't know why? here is the code
    
    $new_price_total = 85.99; // any value from db
    while($new = mysql_fetch_array($get_new_total_res)){
    	if($new['ex_prd_price'] > 0)
    		$new_price_total -= $new['ex_prd_price'];
    }
    $new_price_total = floatval($new_price_total) - floatval($price); // here both values are equal suppose 27.99
    
    PHP:
    Any suggestion?
     
    greatlogix, Feb 19, 2009 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    7.1054273576E-15 is the same as:

    7.1054273576 x 10^-15

    which is a pretty small number. Are you sure you want to get such number?

    Peace,
     
    Barti1987, Feb 19, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    7.1054273576E-15 is not an error, it's a number. Specifically, it's .0000000000000071054273576.

    So whatever you're doing, that's the result it's getting. If you don't want scientific notation, use number_format().

    If you don't have numbers in your database with such high precision, you could be seeing a floating point rounding artifact.
     
    SmallPotatoes, Feb 19, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    For example, E5 is equivalent to 10^5 ( 10 to the power of 5 )

    :: ads2help
     
    ads2help, Feb 20, 2009 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your problem is that floating point arithmetic is not 'exact'.... you're seeing what happens sometimes when you do floating point arithmetic.

    The best thing to do is either keep all of your numbers in integers and then divide by 100, for example, or always round before you display the numbers. But in reality, its better to deal in integers whilever you can.
     
    TwistMyArm, Feb 20, 2009 IP