+ 7
Is there are other numbers like float 0.7 which does not give exact value
6 Respostas
+ 20
First off, let's work with pen and paper!
I want to show you how you can convert 0.7 to its binary equivalent number which is the standard representation of data inside the computers.
So, 0.7 -> binary
In order to get the binary form, you should multiply the number by 2 successively and record the integer parts. So, here we go!
1 : 0.7 * 2 = 1.4 I grab the integer part (which is 1) and continue my work with 0.4
2 : 0.4 * 2 = 0.8 integer is 0. i record that and continue my work with 0.8
3 : 0.8 * 2 = 1.6 integer is 1
4 : 0.6 * 2 = 1.2 integer is 1
5 : 0.2 * 2 = 0.4 integer is 0
Now, you see that the process repeats after 5 steps. Since now, I got 0.10110 (from top to down) as 0.7 equivalent. But to make sure that the pattern is clear enough for you, I'm going to repeat that for another 4 steps.
6 : 0.4 * 2 = 0.8 integer is 0
7 : 0.8 * 2 = 1.6 integer is 1
8 : 0.6 * 2 = 1.2 integer is 1
9 : 0.2 * 2 = 0.4 integer is 0
See, the pattern is repeated for 0110 portions. It means, there will be infinite repetition for that binary part and obviously, the machine can't represent that precisely. Mathematically, we put a bar notation above that four bits. That is,
_____
(0.10110) = (0.10110011001100110...)
Now that it became crystal clear that what's going on behind the scene, let's go ahead to our main topic.
Float (single precision floating point)
Double (double precision floating point)
So, what's the difference?
Well, that's all about how much you'd like a number being stored precisely in a variable. For example, you want to store the value of Pi. Suppose that the float type is capable of holding a floating point number with 7 digits precision and double with 15 digits.
You are forcing to store Pi with 15-digit precision into a float type. Now, What's happening?
It narrows itself to fit into a float type
3.14159265
float pi1 = 3.141592653589793; // narrowed to 3.14159265
double pi2 = 3.141592653589793;
+ 20
//==================================
// Single and double precision spec
// Done for : Omkar Patil
// By : Babak Sheykhan
//==================================
#include <iostream>
#include <iomanip>
#include <limits>
int main()
{
// narrowed to 3.14159265
float pi1 = 3.141592653589793;
double pi2 = 3.141592653589793;
std::cout << "Minimum guarantee of double precision length is " << std::numeric_limits<double>::digits10;
std::cout << std::endl;
std::cout << "Minimum guarantee of float precision length is " << std::numeric_limits<float>::digits10;
std::cout << std::endl << std::endl;
std::cout << std::fixed << std::setprecision(std::numeric_limits<double>::digits10);
std::cout << pi1 << " [After 7th decimal place are garbage values]" << std::endl;
std::cout << pi2 << std::endl;
// represent inside the memory as 0.69999999 or 0.70000001
float a = 0.7f;
// represent inside the memory as 0.69999999999999996 or 0.70000000000000001
double b = 0.7;
std::cout << "a : " << a << " [After 7th decimal place are garbage values]" << std::endl;
std::cout << "b : " << b << std::endl;
if (a < b) std::cout << "a is less than b\n";
else std::cout << "a is greater or equal to b\n";
}
[https://code.sololearn.com/cHso97tyV1WK]
+ 6
This is because you're comparing a float to a double literal. The double is accurate to more decimal places, so it will be greater. Try comparing to a float literal instead (e.g. 0.7f). This is not strange behavior at all.
Because of the way floating point are stored in memory, they are almost never exact. Using datatypes consistently will help you when making comparisons like this, though.
+ 1
yes like 0.5
+ 1
yes
0
using float??