Why is constant 0.7 bigger than float 0.7 in C ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is constant 0.7 bigger than float 0.7 in C ?

https://code.sololearn.com/cZIdMoP5zZDX/?ref=app

11th Aug 2020, 2:54 AM
Jax Young
Jax Young - avatar
5 Answers
+ 9
You shouldn’t test for exact equality of floating point values. Also 0.7 is treated as a double, which uses more bits for precision. In your code you are comparing two different types.
11th Aug 2020, 3:00 AM
Brian R
Brian R - avatar
+ 8
Yes it will print hii because float a = 0.7; The value of a is float type but the type of 0.7 is double and double size precision more than float values . By default decimal values are float types if u want to make it float type then u have to write float a=0.7f dont . Hope you understood
11th Aug 2020, 6:26 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
no. you will have to handle that conversion yourself. if you need, you can write a float by adding “f” to the end of a number: ‘12.6f’ would be a floating point.
11th Aug 2020, 3:08 AM
Brian R
Brian R - avatar
+ 1
Brian R That is to say C won't change num's type when comparing, right?
11th Aug 2020, 3:07 AM
Jax Young
Jax Young - avatar
0
Brian R Thanks. So C won't do conversion and allows comparing between different type. I think it shouldn't happen. In my opinion, it may cause many bug.
11th Aug 2020, 3:14 AM
Jax Young
Jax Young - avatar