:rolleyes:Quote:
Originally Posted by Summonerur
>yeah, something like that.Quote:
Originally Posted by Dave_Sinkula
Uh, no. Not "something like that".Quote:
Originally Posted by Dave_Sinkula
Printable View
:rolleyes:Quote:
Originally Posted by Summonerur
>yeah, something like that.Quote:
Originally Posted by Dave_Sinkula
Uh, no. Not "something like that".Quote:
Originally Posted by Dave_Sinkula
Sorry:
input:
A: 32
B: 34
Expected output: 46.69047012
given output: 46.6905
Just wondering how to stop it from rounding
thanks for all the help you have been giving me btw.
That is everything you need to know about floating point numbers. There is no rhyme or reason to why they round they do. They just do. If they run across something they can't represent, it rounds it. You as the programmer have absolutely no way of easily telling when it's going to round. It just does.Quote:
Originally Posted by Summonerur
As I stated: "Welcome to the world of floating point numbers." If you don't want rounding, don't use floating point numbers. Period.
Trust me, you'll know when you're being "bashed" by me.
Quzah.
Well if you really want everything, then read this
http://cch.loria.fr/documentation/IE...M/goldberg.pdf
There is a default precision for "%f". If you want a different precision, just specify it.Quote:
Originally Posted by Summonerur
Code:#include <stdio.h>
#include <math.h>
int main(void)
{
double a = 32, b = 34, c = sqrt(a * a + b * b);
printf("c = %f\n", c);
printf("c = %.8f\n", c);
return 0;
}
/* my output
c = 46.690470
c = 46.69047012
*/
Well, I suppose from your viewpoint it might seem like Quzah was not trying to be helpful, but if you had taken the initiative and actually thought about the answer he gave you (as opposed to moaning about what a mean and nasty guy he is) you might have come up with the novel idea of looking up some floating point resources for yourself.Quote:
Originally Posted by Summonerur
From the viewpoint of the regulars on this board, you are just another student in a long list of students who come here for a short time and are gone for good. Every year a new batch pops up wanting to get all kinds of good information, in the easiest fashion possible. Well hey, this board is to help people get good info, and yeah it is free, so you can't really complain if you don't like what you get. But here is the kicker - how many times do you think some of the members on the board, like Quzah or Salem or any of the others with 3000+ posts have seen a question like, 'Gee, this floating point stuff is whacked!' ? Believe me, they have seen it many times. And as the answer is not generally a short one (as evidenced by some of the documentation referred to previously and in this post) it is somewhat easier to just give a bit of hint (like Quzah did) which ought to be enough to prompt someone like you to do some looking instead of sitting there like a baby bird with its neck stretched out and its beak open waiting for mommy bird to vomit up some worms or whatever.
That said, here are a couple of other things with regards to your question, should you wish to take the time to look through the material.
http://docs.sun.com/source/806-3568/ncgTOC.html
http://www.eskimo.com/~scs/C-faq/s14.html
This next one might be a little helpful too:
http://stevehollasch.com/cgindex/coding/ieeefloat.html
~/