Why Does Multiplying Floats in C Lead to Unexpected Results?

  • Thread starter Thread starter meldave00
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion centers around the unexpected results encountered when multiplying floating-point numbers in C, particularly focusing on the representation of floats and the implications for accuracy in calculations. Participants explore the nature of floating-point arithmetic, its limitations, and potential workarounds.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue with multiplying two floats, resulting in a value that appears inaccurate due to the way floats are represented in memory.
  • Another suggests switching to double precision as a potential solution to improve accuracy.
  • A participant explains that floats can only represent a limited number of values due to their finite memory size, leading to representation errors.
  • One comment notes that truncation can fix some issues but highlights that certain mathematical truths, like 1+1 equaling 1.999999, cannot be resolved this way.
  • Another participant emphasizes that the problem is inherent to how machines represent numbers, stating that many numbers cannot be represented exactly in floating-point format.
  • It is mentioned that results from floating-point operations are generally approximate and that understanding the underlying issues is crucial for programmers.

Areas of Agreement / Disagreement

Participants express a range of views on the nature of floating-point arithmetic and its limitations, with no consensus on a single solution or understanding of the problem. Some advocate for using double precision, while others emphasize the fundamental issues with floating-point representation itself.

Contextual Notes

Participants acknowledge that floating-point numbers are discrete and cannot represent all real numbers accurately, which contributes to the observed inaccuracies in calculations.

meldave00
Messages
39
Reaction score
0
Hi,

Please see my code below. If I enter 4.42 in for my first float
and 10.00 in for my second float and I muliply them together I get
a result of 44.200001. The reason is because the compiler thinks
that 4.42 = 4.4200001 (I checked this during debug). Why does this
do this? How does anybody get accurate results when working with
C? Anyone know what the work around is for this type of issue?
I'm refreshing my knowledge of C... I'm a little rusty.


#include <stdio.h>

void main(){

float aFloat1; float aFloat2; float aFloat3;

printf(" Please enter the value of float #1: ");
scanf("%f",&aFloat1);

printf(" Please enter the value of float #2: ");
scanf("%f", &aFloat2);

aFloat3 = aFloat2 * aFloat1;

printf("\n");
printf(" The value of aFloat3 is: $%f \n\n", aFloat3);

getchar();
getchar();

};


regards,

meldave00
 
Technology news on Phys.org
go to double (precision)
 
meldave00 said:
Why does this do this?
A float takes up four bytes of memory and therefor can only represent at most 4 billion or so different numbers out of the infinitely many numbers there are. As a result, not all numbers are perfectly represented. By using double instead of float, you greatly increase the ability to represent, but still nowhere near infinite.
meldave00 said:
printf(" The value of aFloat3 is: $%f \n\n", aFloat3);

Try this instead.
printf(" The value of aFloat3 is: $%8.4f \n\n", aFloat3);
 
Float is and always has been problematical in this regard.
In the OP the problem can be fixed by truncation.
But, 1+1 = 1.999999 is not going to get fixed this way.
 
This has nothing to do with C. It's a property of the machine and how it represents numbers.

See http://docs.sun.com/source/806-3568/ncg_goldberg.html

Floating point numbers are not continuous--they're discrete. There are a lot of numbers (I would guess most of them) that cannot be represented exactly by a finite number of digits. In base 10, think square root of two, or 1/3. In the usual binary floating point representation, I believe 1/10 cannot be represented in a finite sequence of digits.

So remember, your results are almost always approximate. It may not even be that good an approximation, and there are ways to blow away your approximation in a hurry. If you keep that in mind, you'll be ahead of most scientific programmers. "Go to double" is the standard answer. But if you don't understand the underlying problem, you'll get bit there, too.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
14
Views
4K
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 21 ·
Replies
21
Views
9K
Replies
4
Views
3K