0

I'm trying to make IF condition where I'm trying to print some text when number is 'correct'. The question is what if the condition can't have 'exact number' ? Like when you are finding everything (*) in linux system "find / -name *.mp3".

Here is my code, sure it do not work work for bad syntax but I put it here for example about my question:

#include <iostream>

int main() {
        double b = 6.922083e-07;
        if (b == 6.922.*e-07) {
                printf("LOL \n");
        }
}

.* by this I mean everything other which do not matter.

Thank you very much, will be happy as an elephant if you can help me. I'm stupid and I just can't find it on google.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
SimonLi
  • 13
  • 4
  • I don't understand your abbreviation: please show the exact code. – Weather Vane Mar 14 '20 at 20:55
  • You can subtract the numbers and see if the difference is less than a threshold. It won't work with file names though. – François Andrieux Mar 14 '20 at 20:55
  • remember that floating point numbers are an approximation. Thus equality is not defined for them. you should compare the difference to some epsilon. For example, `fabs(b - b1) < epsilon` where epsilon might be defined to be 1E-7 – thurizas Mar 14 '20 at 20:57
  • @thurizas still, if the unclear code is intended to be `if (b == 6.922083e-07)` which is comparing a `double` variable with a `double` literal, AFAIK the conversion is guaranteed to be the same as with `double b = 6.922083e-07;` – Weather Vane Mar 14 '20 at 20:59
  • @thurizas: I agree epsilon comparison should be used. On the other point I disagree: equality is defined for `double`. – BlueTune Mar 14 '20 at 21:01
  • Please don't tag with languages other than the one being used. Fixed. – ikegami Mar 14 '20 at 21:02
  • 1
    You can't use the analogy of `ls abc*.*` that helps you find files starting with letters "abc" to numbers. Closest match to the task you want to achieve may be `if (b >= 6.922e-07 && b < 6.923e-07) ...`. This will help identify whether `b` starts with `0.0000006922` or not. – ssd Mar 14 '20 at 21:05
  • 1
    @BlueTune -- epsilon comparisons are only appropriate when they are appropriate. Don't forget, "nearly equal" (i.e., epsilon comparison) is **not** like equality. If a "nearly equal" b and b "nearly equals" c it does not follow that a "nearly equals" c. Ultimately, you have to know where your numbers come from and what you want to do with the result in order to decide how to compare floating point values sensibly. – Pete Becker Mar 14 '20 at 21:12
  • Does this answer your question? [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) –  Mar 14 '20 at 21:20
  • `6.922.*e-07` doesn't make any sense. Did you mean `6.922e-07`? – Aykhan Hagverdili Mar 14 '20 at 21:29
  • @Ayxan The OP explicitly addresses what they mean by that notation in the question. – Brian61354270 Mar 14 '20 at 21:30
  • @Brian *"by this I mean everything other which do not matter."* I am still not sure what that means or what is being asked. – Aykhan Hagverdili Mar 14 '20 at 21:32

2 Answers2

0

You can write a function to automate the task of getting the exponent:

int get_expo(double num){
  int n = 0;
  double number = num;
  if(number > 1 && number !=0){
      while (number > 1){
          number/=10;
          n++;
      }
      n-=1;
   }else{
       while(number < 1){
            number*=10;
            n++;
       }
       n = -n;
  }
  return n;
}

And then you can multiply that number by the 10^exponent plus 3 if you want three digit precision and then convert it to int:

#include <math.h>
int converted_num = d*pow(10 , get_expo(d) + 3);
if(converted_num == 6922)
{
   //Do something
}

Note that I used 6922 instead of 6.922

Parsa Mousavi
  • 1,052
  • 1
  • 13
  • 31
0

If you want "close enough" to be 1e-8, for example:

double eps = 1.0e-8;
if (b > (6.922083e-07 - eps) && b < (6.922083e-07 + eps)) {
    printf("LOL \n");
}
stark
  • 12,615
  • 3
  • 33
  • 50