Jump to content

[SOLVED] Exit script diffent outcome when change variable in if statement ?


stevem123uk

Recommended Posts

Hi

Need some help with the script below.

I can not understand why when i change the $check1 variable to $check in the if statement the outcome is different.

 

Any help would be appriciated.

 

 

<?php

$lastcheck = 1.4;

$check1 = $lastcheck;

$check1 = $check1 + .7 ;

$check = 2.1;

$dt = 2.1;

if ($dt == $check1) die ("Stop and display this.<br>dt = $dt<br>lastcheck = $lastcheck<br>check = $check<br>check1 = $check1");

?>

The problem with float type is small errors always creep in during calculations.

 

When using floats, don't rely on the "==" operator. Instead you need to check if the difference is tolerably small, so you need something like

 

if (abs($dt - $check) < 0.00000001)

 

 

 

I added a line to your code before your if()

 

if ($check != $check1) die ($check - $check1);          // --> 4.4408920985E-016

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.