|
-
July 17th, 2005, 12:43 AM
#1
Why this error happen?
Code:
float a=127.2344,c;
int b;
b = abs(a); <====1
c = a - b; <====2
1 - Return 127
2 - Return 0.234375 and the correct is 0.2344 - ERROR.
Why this problem happen? I really need the correct value. óÒ
Tks very much ^^
-
July 17th, 2005, 01:14 AM
#2
Re: Why this error happen?
 Originally Posted by phoenixbr
Code:
float a=127.2344,c;
int b;
b = abs(a); <====1
c = a - b; <====2
1 - Return 127
2 - Return 0.234375 and the correct is 0.2344 - ERROR.
Why this problem happen? I really need the correct value. óÒ
Tks very much ^^
here is your answer
Code:
double a=127.2344,c;
int b;
b = abs(a);
cout<<b;
c = a - b;
cout<<endl;
cout<<c;
due to decleration of float you was getting some thing
because compiler will treat it as double
-
July 17th, 2005, 01:53 AM
#3
Re: Why this error happen?
-
July 17th, 2005, 01:57 AM
#4
Re: Why this error happen?
 Originally Posted by phoenixbr
Tks very much ^^
you welcome
-
July 17th, 2005, 01:59 AM
#5
Re: Why this error happen?
Read this FAQ. Hope this helps.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 17th, 2005, 03:04 AM
#6
Re: Why this error happen?
 Originally Posted by phoenixbr
Code:
float a=127.2344,c;
int b;
b = abs(a); <====1
c = a - b; <====2
1 - Return 127
2 - Return 0.234375 and the correct is 0.2344 - ERROR.
Why this problem happen? I really need the correct value. óÒ
Tks very much ^^
There are two implicit type conversion occured;
and the complier will transform the type of expression to assigned object.
Good Luck!
-
July 17th, 2005, 03:18 AM
#7
Re: Why this error happen?
 Originally Posted by phoenixbr
Code:
float a=127.2344,c;
int b;
b = abs(a); <====1
c = a - b; <====2
- It converts from float to integer, then it computes the absolute value of the integer.
To avoid strange rounding rules, it may be preferable to use fabs which uses a double argument.
- It is a precision error, because 127.2344 cannot be correctly represented on a float number (float are binary numbers, not decimal numbers, so 1/5 for example cannot be correctly represented).
Using double values greatly increase the precision.
Note, that BC++ has a library for BCD (Binary coded decimals) floats, but use them only if you really need them, because it is not portable.
-
July 17th, 2005, 06:12 AM
#8
Re: Why this error happen?
You can use floor and fabs functions - it wouldn't require conversions:
Code:
double a=127.2344;
double c=fabs(a)-floor(fabs(a));
"Programs must be written for people to read, and only incidentally for machines to execute."
-
July 17th, 2005, 06:59 AM
#9
Re: Why this error happen?
You could also use boost::rational if you really want decimal fractions represented exactly.
An IEEE float gives 23 significant bits of data (22 bits but there is one assumed 1-bit) so that's about 8 million. Therefore you get just under 7 significant figures in decimal.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|