3

Note: i think this is not duplicate because same double adding works in another script and not in same script. and these are all double not float, and that question is not related to php, if that's the problem then what could be solution in php.

while both script var_dump same number and type is showing float with same value but calcuation is diffrent in both script.

what could be wrong? is there anything else hidden in double or float which don't get revealed in var_dump?

i know i can round things to fix problem, but why its working in one script and not in another.

i've given full script with simplified version.

full version which don't works https://eval.in/976389

short version which works fine https://eval.in/976394

why both giving different output?

$str="12000,1000,5000,-5000,25000,-10000,-10000,-4000,-10000,-3000,7000,-3000,-3000,-1000,7000,13000,-10000,-7000,-3000,-20,3
2,-1000,1000,2000,1000,-100,-100,-100,-3646,3000,-2000,-19,-9,3000,2000,-38,-2000,-200,-3646,-48,-100,3000,-1000,-1500,
-20,-80,4000,-46,-200,-3646,-183,1000,-67,10000,-7000,-1000,-2000,-500,10000,-4000,-2200,-100,-557.28,-16,1.09,1.05,-50
,-110,2000,-50,-231,-3646,-1000,-30,-50,-50,-110,-30,50,30,-50,-32,110,-28,-52,-32,-32,50,-102,-102,-300";
$numbers=explode(',',$str);

$balance=0;
foreach ($numbers as $number){
    $number=doubleval($number);

    $l=$balance;
    $balance=$balance+$number;
    if(strlen(@explode('.',$balance)[1])>2){

        var_dump($l);
        var_dump($number);
        echo $l.' + '.$number."\n";
        echo ($l+$number)."\n";die;
    }
}
  • 3
    Please provide proper description, also provide the script instead of the images. – Darshan Jain Mar 22 '18 at 06:58
  • the main script is too long.. but for ease i've given var_dump and die in code.. so let me know if i am missing something – Marty Krishner Mar 22 '18 at 07:01
  • Same things has happened to me one of my project too. Still I don't know the reason for it – I am the Most Stupid Person Mar 22 '18 at 07:01
  • then please upvote and share question so we can find solution, last 5-6 hours i am trying but not able to find solution.. – Marty Krishner Mar 22 '18 at 07:02
  • @MartyKrishner I already up-voted... But please share all the codes at least using http://phpfiddle.org/.... I could not do it because their are lots of DB called in my script – I am the Most Stupid Person Mar 22 '18 at 07:04
  • @darshanJain i've added full code now. – Marty Krishner Mar 22 '18 at 07:04
  • @MartyKrishner sav.csv is a confident file? If not please share it tooooo – I am the Most Stupid Person Mar 22 '18 at 07:05
  • ok let me do it on phpfiddle.org – Marty Krishner Mar 22 '18 at 07:10
  • @DarkBee there is double in mycase. – Marty Krishner Mar 22 '18 at 07:10
  • Hi, i've give now script without csv and all other logic which still causing this. – Marty Krishner Mar 22 '18 at 07:25
  • 1
    @MartyKrishner the difference between `double` and `float` is the amount of memory used to store the values (`8` vs `4` bytes). The format is the same, the problems are the same. To understand why it works this way read [this question](https://stackoverflow.com/q/588004/4265352). The solution for your specific case is to use [`number_format()`](http://php.net/manual/en/function.number-format.php) when you display the values (only when you display them, using it for intermediate calculations is only a waste of CPU power). – axiac Mar 22 '18 at 07:30
  • but 0.00000001 is meaning full value which can't be ignore, i don't think i sholud format number i want presics number to display.. if i add use 10 decimals then my results will be 10 decimal but i used only 1 decimal then result sholud also be 1 decimal i don't think here number_format i sholud use. – Marty Krishner Mar 22 '18 at 07:34
  • If those are currencies: use [bcadd](http://php.net/manual/de/function.bcadd.php) to add those numbers (as strings, don't convert them to float/double), with arbitrary precision. – Nitori Mar 22 '18 at 08:01
  • those are just numbers... as per math what will be 316.86+ (-300) will be 16.860000000001 or just 16.86, i want to know what i am doing wrong here. – Marty Krishner Mar 22 '18 at 08:35
  • @DarshanJain Can you check and reconisder making this post as duplicate. see the question again please. i've give two script in one it works and in another it don't, its not question like that duplicate one... – Marty Krishner Mar 22 '18 at 08:41
  • Hi, @yoshi can you please check question again, before marking it as duplicate if its duplicate then let me know how to fix the issue – Marty Krishner Mar 22 '18 at 08:42
  • Wow, moderators. you didn't even read the question and marked duplicates, while problem is that if its float presion problem then why it works well in one script and not in another.. that is my question, i know float presion issue in general computing. – Marty Krishner Mar 22 '18 at 09:09

0 Answers0