0

for example

double x = -1;
TrySetX(ref x);
if(x == -1)
    //since x == -1, it obviously wasn't set
    TryADifferentWayToSetX(ref x);

Use(x);

in the case where x is not changed, will x == -1 always return true, or will I have to use an epsiolon for the comparison?

The logic here is that both literals will presumable be converted to the same value, so there is no need to worry about the fact that they may lose precision.

Yair Halberstadt
  • 5,733
  • 28
  • 60
  • 4
    wouldn't it have been quicker just to try it for yourself? – ADyson Oct 30 '18 at 15:20
  • 1
    Why do you provide x by ref when you don´t change it? – MakePeaceGreatAgain Oct 30 '18 at 15:20
  • @Adyson. i have, and it works. But that might just be a fluke - what about for a different architecture, or a different literal – Yair Halberstadt Oct 30 '18 at 15:21
  • Try it again using an irrational number and see what happens. –  Oct 30 '18 at 15:23
  • 1
    How about making `DoSomethingThatMightChangeX` a function that returns a `double?` then check for `x.HasValue` ? As is you are depriving yourself from using the Value -1. Is that reasonable? – Fildor Oct 30 '18 at 15:23
  • Possible duplicate of https://stackoverflow.com/questions/485175/is-it-safe-to-check-floating-point-values-for-equality-to-0/485741#485741 – Chris Akridge Oct 30 '18 at 15:24
  • @Fildor. perfectly. However since this was in a high performance part of the code, I was hoping to save a byte by avoiding using nullable doubles. However I suppose I could use double.NaN instead – Yair Halberstadt Oct 30 '18 at 15:25
  • @HimBromBeere I was giving an example of a method that may or may not change X, and I want to check afterwards if x was changed, by checking if x equaled -1 - it's original value – Yair Halberstadt Oct 30 '18 at 15:28
  • 2
    It doesn't matter where you use literal, same literal will produce same value for the same type. Even if you [lose precision](https://stackoverflow.com/q/1398753/1997232), they **both** will do, so comparison will still work. – Sinatr Oct 30 '18 at 15:39
  • @Sinatr That's what I though. HimBromBeere seems to think differently though. Do you want to add that as an answer? – Yair Halberstadt Oct 30 '18 at 15:41
  • @YairHalberstadt, I've read his answer several times, but I don't understand what he say (therefore my comment, which I hope is crystal clear). In his code `DoSomething` will be called always if you don't change `x`. – Sinatr Oct 30 '18 at 15:44
  • One pitfall I can think of is using different literals, e.g you may think what `double x = 1 * 0.1` is same as `double x = 100000 * 0.000001`, nope they are different, the latter is `0.099999999999999992`. – Sinatr Oct 30 '18 at 16:05

1 Answers1

0

Trying it out, it looks like both literals get converted to the same value. As such the comparison is safe:

Here is a little test I wrote

public class Program
{
    public static void Main(string[] args)
    {
        double x = 3.455678756567656765677812345678912345678901234567890123456789009876543223456798765423456709876512345;
        System.Console.WriteLine(x);
        System.Console.WriteLine(x == 3.455678756567656765677812345678912345678901234567890123456789009876543223456798765423456709876512345);
    }
}

That number is long enough there will be a large loss in precision, but it prints

3.45567875656766
True

So indeed there is loss of precision, but the comparison nonetheless works.

Yair Halberstadt
  • 5,733
  • 28
  • 60