0

What's wrong to use operators while you are using floating point numbers. Can't we use '==' '<=' etc operators with floating point numbers?

here is the code.

# include <iostream>

using namespace std;

main(){
 float x, y, z;
 cout<<"1st integer: ";
 cin>>x;

 do {
    cout<<"2nd integer: ";
    cin>>y;
    if(y<=0 ){
            cout<<"You can't divide by zero"<<endl;
            continue;
        } else {
                break;
                }
        } while (1);

z = x/y;
cout<<"Result: "<<z;


}

it generate right result as i want to get. But from some where i heard that's not a good logic to use operators with floating point numbers. why?

  • Relevant read: http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate `0.1 + 0.2 == 0.3000...4` and is `> 0.3` (not `== 0.3`). – Caramiriel Feb 23 '15 at 06:57
  • @Galic, actually it is not unlike that two floating point #s are exactly equal. In fact, it's probably more likely that they are equal than if you had infinite precision. For example: float a = 1.0f, b = 1.0f; will surely make a==b. additionally, a==(b+delta) is also true if delta is below the precision of float. what is problematic is that sometimes they're not equal when you expect them to be... – thang Feb 23 '15 at 07:03
  • @TonyD But what will be happened when i use this program in integer variable instead of floating point? I mean if i input the numerator value by 0.50, the loop doesn't break, it continuously runs and display the results, why? Integer drops the decimal numbers, so why it doesn't implement here? – Salenymin Majid Feb 23 '15 at 07:38
  • If you change `y` to `int`, then typing `0.50` results is `0` being parsed into `y` which triggers the "can't divide by zero" message and `continue`, then it tries to parse the "." into an `integer` which is an input error... that doesn't affect `cin` or `y` and you're stuck in the loop. You should search for some examples of testing for and handling input errors i.e. `if (cin >> y) ... else { cin.ignore(std::numeric_limits::max, '\n'); std::cin.clear(); };`. Lots of SO questions explain this. – Tony Delroy Feb 23 '15 at 07:48

2 Answers2

-1

What you're doing is fine. What you do need to be wary of is using the equality operator == with floating point, because the results may surprise you (eg 0.1 + 0.2 != 0.3).

But there's no problem with using >, >=, <=, <.

Community
  • 1
  • 1
John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 2
    *"But there's no problem with using >, >=, <=, <."* - not so - e.g. on [ideone.com here](https://ideone.com/rgZM4j) `.1 + .2 > .3` while `.3 * .3 < .9`. – Tony Delroy Feb 23 '15 at 07:26
  • 1
    @TonyD the first one is a correct example, but `.3*.3` *is* smaller than `.9`, and here `.3*.3==.09` correctly evaluates to true. A better example might be `.2*.2>.04` – KillianDS Feb 23 '15 at 08:39
  • @KillianDS: oh yikes - yeah - rushing to demonstrate stuff you already know, it's easy to be careless... thanks. – Tony Delroy Feb 23 '15 at 10:06
-1

You can compare floating point numbers safely against precisely zero. This will prevent a division by zero.

What you can't do is check x==0.1 and then divide by x-0.1. Since 0.1 isn't exactly representable, you have rounding in a generally unknown direction.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • This is a bad example. If `x` doesn't compare equal to `0.1`, then `x-0.1` will not compare equal to `0.0`, and can safely be divided by. – TonyK Feb 23 '15 at 08:44
  • @TonyK: Your statement is true for IEEE754 which has bit-perfect rounding for the 4 basic arithmetic operations, but C++ does not mandate IEEE754. – MSalters Feb 23 '15 at 08:46
  • `x-0.1` will be _very_ non-zero -- in the worst case, about `0.05 * epsilon` in absolute value. This is `0.1*2^-24` in IEEE 32-bit format; for this format, even without using denormalised numbers, the smallest representable positive number is `2^-126`. This huge difference will not disappear for any realistic floating-point representation. – TonyK Feb 23 '15 at 08:59