1

For some weird reason this if statement is not finding a match, when the values are clearly equal to the $change_pct_candle values as shown in var_dump below

if($change_pct_candle[1] >= 0.38022813688214 && $change_pct_candle[2] >= 1.3487475915221) {} 

If I try:

if($change_pct_candle[1] >= 0.38022813688214) {} 

It works. If I try:

if($change_pct_candle[2] >= 1.3487475915221) {}

It works. When I try:

if($change_pct_candle[1] >= 0 && $change_pct_candle[2] >= 1) {} 

It works. When I try:

if($change_pct_candle[1] >= 0.38022813688214 && $change_pct_candle[2] >= 1.3487475915221) {} 

It doesn't work. Am I doing something wrong here?

The var_dump of $change_pct_candle is:

array(7) {
  [0]=>
  float(-0.10984848484848)
  [1]=>
  float(0.38022813688214)
  [2]=>
  float(1.3487475915221)
  [3]=>
  float(-0.57471264367815)
  [4]=>
  float(0.19193857965451)
  [5]=>
  float(0.19230769230769)
  [6]=>
  float(0.1926782273603)
}
Sven Kahn
  • 487
  • 1
  • 3
  • 16
  • your unknown `if` is failing you somehow. Plus, arrays are zero-based. – Funk Forty Niner Nov 07 '16 at 18:03
  • @Fred-ii- what is unknown? And if you mean how arrays start at 0, yes, as you see in the var_dump I am using == value of [1] – Sven Kahn Nov 07 '16 at 18:04
  • `If() statement`, where's that? All you included was `&&`. – Funk Forty Niner Nov 07 '16 at 18:05
  • I thought it would be assumed that these were in an if() statement @Fred-ii- – Sven Kahn Nov 07 '16 at 18:05
  • Updated to include if() – Sven Kahn Nov 07 '16 at 18:06
  • And by not working, you mean?? – Hackerman Nov 07 '16 at 18:06
  • @SvenKahn You haven't defined what you mean by "works" or "doesn't work". What are you doing to determine that? – Mike Nov 07 '16 at 18:07
  • if(statement) { die(); } - just to see if it matches – Sven Kahn Nov 07 '16 at 18:07
  • Try an `||` (OR) statement then. Hard to say what you're comparing it against. You only showed the array. Maybe I'm not grasping something though; see if others can pick up on it. – Funk Forty Niner Nov 07 '16 at 18:09
  • @Hackerman not finding match is what I mean. I think this is all pretty obvious, I didn't think it had to be completely spelled out. – Sven Kahn Nov 07 '16 at 18:09
  • @Fred-ii- it can't be OR, it must be AND – Sven Kahn Nov 07 '16 at 18:09
  • 2
    Looks to me like a [float precision](http://php.net/manual/en/language.types.float.php) problem. – Mike Nov 07 '16 at 18:11
  • 3
    I'm not seeing an issue: [https://3v4l.org/6amMp](https://3v4l.org/6amMp). What version PHP are you using? – Jonathan Kuhn Nov 07 '16 at 18:12
  • It wasn't...at first I think that you where trying to perform bitwise operations...also there was not sample input vs expected output behavior, and that was why everyone was asking you about, what `is not working` means :) – Hackerman Nov 07 '16 at 18:13
  • @JonathanKuhn yes that works for me as well, and works testing on my server. Looking into this. – Sven Kahn Nov 07 '16 at 18:14
  • @Mike yes it seems to be float problem. When I reduce numbers down to x.xxx it works fine. There might be a bug in the way numbers get stored when adding to array in the method I am adding them, really unsure, looking into it further – Sven Kahn Nov 07 '16 at 18:18
  • @JonathanKuhn PHP 7.0.11 - your example works, and on my server it works. But when I reduce the float down to x.xx in my actual code, it works, I think there is a bug in how values are getting added to the array – Sven Kahn Nov 07 '16 at 18:20
  • 1
    @SvenKahn In the link I posted above: *"So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available."*. See http://php.net/manual/en/ref.bc.php and http://php.net/manual/en/ref.gmp.php. – Mike Nov 07 '16 at 18:20
  • @Mike thank you for this, looks like I have to change my code to reduce the float – Sven Kahn Nov 07 '16 at 18:22
  • 1
    Or you could try rounding the numbers to, say, 3-4 decimal places. I'm not sure at what point PHP loses its floating point precision, but I think it should probably be good in those cases. – Mike Nov 07 '16 at 18:22

1 Answers1

-1

The reason is "Floating point numbers have limited precision". Just have a look at this page from php.net and pay close attention to the warning about this concept. That'll explain why , and hopefully what to do. http://php.net/manual/en/language.types.float.php

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality.

Maximo
  • 186
  • 5
  • 1
    This was already mentioned in a comment above. – Mike Nov 07 '16 at 18:26
  • Well, probably because that's the key to the solution of your problem – Maximo Nov 07 '16 at 18:27
  • Yes @Mike figured this out first, so I don't feel great accepting the answer from you. But yes, you are right on it. – Sven Kahn Nov 07 '16 at 18:28
  • @SvenKahn I can't post an answer because you haven't posted a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). I think answers almost always should explain what's wrong and also provide working code to fix it. – Mike Nov 07 '16 at 18:30