0

I use a simple code to create a series of images from one file, depending on the condition. While doing so I realized that:

splot [1:150][1:150][] "dinozaur" u 5:6:(($4==0.4+0.0025*3)?$2:1/0)

...does not work but:

splot [1:150][1:150][] "dinozaur" u 5:6:(($4==0.40750)?$2:1/0)

...works.

However, both

splot [1:150][1:150][] "dinozaur" u 5:6:(($4==0.5+0.0025*3)?$2:1/0)

...and

splot [1:150][1:150][] "dinozaur" u 5:6:(($4==0.50750)?$2:1/0)

work.

If I get an error, its the one in the title. Is there some kind of obvious mistake that I am doing ?

Im enclosing a snippet of data file with column 4 having values of 0.40750 https://www.dropbox.com/s/lpv0m2wfoo3qwl7/dinozaur?dl=0

mgaak
  • 3
  • 2
  • Welcome to Stackoverflow. Please edit your question to clarify your problem a bit. This might help you finding answers fast. For guidance please read [how to ask questions](https://stackoverflow.com/help/how-to-ask) and [how to create a minimal example](https://stackoverflow.com/help/mcve) – 5th Sep 11 '18 at 14:31

1 Answers1

0

Floating point arithmetic on a computer is not exact. For example, 0.4+0.0025*3 might not be exactly the same as 0.40750. For detailed information, take a look at this question: Is floating point math broken?

You generally want to avoid testing floating point numbers for strict equality. Instead, you can test is two numbers are sufficiently close. For example,

eps = 1E-10
splot [1:150][1:150][] "dinozaur" u 5:6:((abs($4 - (0.4+0.0025*3)) < eps) ? $2 : 1/0)

should work.

user8153
  • 4,049
  • 1
  • 9
  • 18