Reputation: 19
So, I am reading a C prog. book and I read this exercise:
Write a program which asks the user to enter a dollars-and-cents amount, then displays the amount with 5% added?
Solution :
#include <stdio.h>
int main(void) {
float original_amount;
printf("Enter an amount: ");
scanf("%f", &original_amount);
printf("With tax added: $%.2f\n", original_amount * 1.05f);
return 0;
}
I know what .3f
means (there should be 3 digits after...), but what does 1.05f
mean?
Upvotes: 1
Views: 1759
Reputation: 111369
The program is apparently using multiplication by 1.05f
as a way to add 5% to a number. But, because of representation error 1.05f is not exactly 1.05; it's a single-precsion floating point number close† to 1.05.
The float
value closest to 1.05 is 1.0499999523162841796875
(assuming the usual 32-bit float format). Since you round the results you would have to use some fairly big numbers to see the effects of the error; try entering 100000000 when the program asks for amount:
Enter an amount: 100000000
With tax added: $104999992.00
If you used double precision instead of single precision, that is, double
instead of float
and 1.05
instead of 1.05f
, the representation error would be smaller but it would still not be exactly 1.05, since this number cannot be represented exactly as the binary floating point numbers that our computers use.
You would get a correct result for 100000000, but still "incorrect" results for astronomically big numbers.
†) How close? From the standard:
For decimal floating constants, and also for hexadecimal floating constants when
FLT_RADIX
is not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner.
Upvotes: 1
Reputation: 5239
printf
has the prototype as int printf(const char *restrict format, ...);
, it uses the const char *restrict format
string to format the data printed.
You are confused between the format specifier %.2f
which is passed as the 1st parameter to printf
and the 1.05f
passed as part of argument list. As you point out, first one is used for formatting. The argument list 1.05f
is used for calculation purposes.The f
indicates to the compiler you want to use a float
or else by default it will be considered double
datatype and the result of original_amount * 1.05f
will be stored in a double
.
It is sufficient to use a float
when you know the number would fit in the float
range.And to indicate this, you append a f
to numbers in the argument list
Upvotes: 0
Reputation: 2909
The 1.05f
does denote a floating point number with value approximately 1.05 (which is 105% = 100% + 5%). The %.2f
is a format specifier and is something very different.
The multiplication with this number actually adds 5% to the value (value * 1.05 = value * (100% + 5%) = value + value * 5%).
Format specifiers occur in the first parameter of printf
-like functions and tell the function how to output the argument corresponding to its position.
Upvotes: 7
Reputation: 5432
it means 1.05 as float float you can take the f away it should work
Upvotes: 0
Reputation: 4411
It's the 5%
part of your exercice. It's equal to: original_amout + (original_amout * 5.0 / 100.0).
Upvotes: 0