Why Does Multiplying Floats in C Lead to Unexpected Results?

  • Thread starter meldave00
  • Start date
  • Tags
    Program
In summary, The conversation is about the issue of accuracy in working with floats in C. Due to the fact that floats can only represent a limited number of numbers, not all numbers can be accurately represented. This causes discrepancies in results, as seen in the example of multiplying 4.42 and 10.00. The solution is to use double precision, although this is not a perfect fix either. The issue is not specific to C, but rather a property of how machines represent numbers.
  • #1
meldave00
39
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
  • #2
go to double (precision)
 
  • #3
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);
 
  • #4
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.
 
  • #5
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.
 

What is a simple C program?

A simple C program is a set of instructions written in the C programming language that performs a specific task or set of tasks. It often includes variables, functions, and control structures to manipulate data and produce a desired output.

How do I start writing a simple C program?

To start writing a simple C program, you will need a text editor and a C compiler. Choose a text editor that you are comfortable with and write your code using the C syntax. Then, use a C compiler to translate your code into machine-readable instructions that can be executed by a computer.

What are some common errors I might encounter while writing a simple C program?

Some common errors in C programming include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code does not follow the proper syntax of the C language. Logical errors occur when the code does not produce the desired output. Runtime errors occur when the code tries to perform an invalid operation, such as dividing by zero.

How can I debug my simple C program?

To debug a simple C program, you can use a debugger tool that allows you to step through your code and check the values of variables at different points. You can also use print statements to output the values of variables and see where the code is not functioning as intended.

Are there any online resources that can help me with my simple C program?

Yes, there are many online resources available to help you with your simple C program. Some helpful resources include tutorials, forums, and online communities where you can ask for help and learn from others. You can also refer to the official C language documentation for more information and examples.

Similar threads

  • Programming and Computer Science
Replies
4
Views
736
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
21
Views
8K
Back
Top