Jump to content

for example if we write this simple code
 

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	float x=0,y=0;
	cout<<fixed<<setprecision(90);
	cin>>x>>y;
	cout<<x<<endl<<y<<endl;
	cout<<endl<<x+y<<endl;

return 0;
}

the expected output should be:
0.1
0.3

0.4

 

but the actual output is:

0.100000001490116119384765625000000000000000000000000000000000000000000000000000000000000000
0.300000011920928955078125000000000000000000000000000000000000000000000000000000000000000000

 

0.400000005960464477539062500000000000000000000000000000000000000000000000000000000000000000

 

this problem gives me issues with a program that has lots of conditions and floating-point arithmetic operations 

is there any way to workaround this problem?

like making the compiler completely ignore everything after sixth digit after the point?
 

 

Link to comment
https://linustechtips.com/topic/610631-floating-point-menace-need-help/
Share on other sites

Link to post
Share on other sites

2 minutes ago, ddddanil said:

What compiler are you using?

i believe it's GNU GCC
i only use C++ with visual studio 2010 and codeblocks 13.12

 

3 minutes ago, ddddanil said:

 

And  x = 1.0, y = 1.0 can help because compiled doesn't translate values very well.

um how would they help? my problems are with the floating points so i'm not actualy using integers

Link to post
Share on other sites

VS uses microsoft compiler. It's good too.

The thing is 1 is a integer constant and 1.0 is an floating point constant. In your code compiler sees float (x) = int (1) and translates int to float.

This problem is more common on languages like JavaScript. That's how your computer calculates float nums. Try to use integers. If you're measuring distance use x^2+y^2 and root them at the end and so on.

Link to post
Share on other sites

53 minutes ago, TheGuyNL said:

Floats are stupid, try using a double instead.

shit happens everywhere,i start to feel that there's no workaround it
but i get to wonder how would any scientific calculator work then? since we have never saw this problem in them ever

Link to post
Share on other sites

Floating point arithmetic is a lot more complex then most ppl think. Something as simple as 0.1 cannot be accurately represented in floating point, only a approximation, which leads to the behavior you observe.

 

Explore this site for further information as it's a complicated subject: http://floating-point-gui.de/

 

And if you have time to spare:

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

 

Quick tips:

-Never compare floating point numbers with `is equal to (==)` but use ranges.

-In loops, think carefully about possible rounding errors and how they might accumulate into a very large error.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×