1

I'm new to C++ and messing around with functions. I have two cpp files and one header. One of my cpp files is main.cpp where the following code is:

a->change(42.57);

I then have another cpp where the following code for change is:

bool Student::change(float mark)

and then the header where the following code is:

bool change(float mark);

When I pass the float in, it is 42.57. However, after it is passed in it suddendly chnages to 42.5699997. Could someone please explain why this happens?

Song879
  • 43
  • 9

1 Answers1

3

42.57 is a double type. (The nearest double is 42.57000000000000028421709430404007434844970703125).

Use 42.57f for a float type. (The nearest float is 42.56999969482421875).

Both therefore are approximations (the float is a looser approximation than the double) but at least your debugger will display the float more appropriately.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483