1

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?

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48
  • 8
    It's just a floating point literal. – awesoon Jul 19 '13 at 10:04
  • 2
    `1.05f` means "float constant with value of 1.05". Anyway, it's not really neccessary, it would work just fine with plain `1.05`. – Jan Spurny Jul 19 '13 at 10:06
  • I would be extremely wary of any textbook that says the solution to a dollars-and-cents problem should *ever* involve floating point. – Alex Celeste Jul 19 '13 at 11:01
  • @JanSpurny There are practical consequences to the choice of `1.05f` and `1.05`. First, these values do not compare equal (the declaration `int x = 1.05f == 1.05;` sets `x` to `0`). Second, `printf()` takes a double, so the computation will effectively be less likely to astonish with `1.05` (although using binary floating-point for fractional monetary amounts is still a bad idea). – Pascal Cuoq Jul 19 '13 at 11:37
  • @PascalCuoq I agree completely, but as this is clearly a beginner's example it would just make it harder to understand (for a beginner). – Jan Spurny Jul 19 '13 at 13:05
  • 1
    you should read this [Suffix of “f” on float value?](http://stackoverflow.com/questions/5026570/suffix-of-f-on-float-value) – Grijesh Chauhan Jul 21 '13 at 21:21

6 Answers6

7

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
urzeit
  • 2,863
  • 20
  • 36
2

1.05f is a float type that has value 1.05

D.Pham
  • 199
  • 10
1

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • 1
    In a good implementation, the value of `1.05f` is the floating-point value closest to 1.05. However, the C standard does not guarantee this, and people should be aware of that. – Eric Postpischil Jul 19 '13 at 13:07
  • Thanks @EricPostpischil, the laxness of the standard never ceases to surprise me :D – Joni Jul 19 '13 at 13:59
0

It's the 5% part of your exercice. It's equal to: original_amout + (original_amout * 5.0 / 100.0).

nouney
  • 4,363
  • 19
  • 31
0

it means 1.05 as float float you can take the f away it should work

Engine
  • 5,360
  • 18
  • 84
  • 162
  • Technically converting in two stages exact decimal->double precision float->single precision float can cumulate 2 round off errors and is thus different from a single conversion exact decimal->single precision, see an illustration here: http://stackoverflow.com/questions/13276862/c-c-notation-of-double-floating-point-values/13279512#13279512 – aka.nice Jul 19 '13 at 16:42
0

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

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59