1

What is a correct way to convert double to float in c++. Is the conversion implicit?

Question 1: Consider double d = 5.0; and float f;

Which one is correct?

  • f = d;
  • f = (float)d;
  • f = static_cast<float>(d);

Question 2: Now consider we have

char *buffer = readAllBuffer(); 
double *d = (double*)(buffer + offset);
float f;

Which one is now correct?

  • f = d[0];
  • f = (float)d[0];
  • f = static_cast<float>(d[0]);

Thanks in advance!

12
  • 1
    None of them are really correct considering a double can store more than a float can so can you loose precision or the value completely Commented Jun 4, 2018 at 13:46
  • 2
    The two questions are really the same. You start with a double value in either case and want to convert to a float value. In other words, how you obtain the double is independent of converting it to float. Commented Jun 4, 2018 at 13:47
  • @NathanOliver Loosing precision is not problem here. which one gives me the closest number? Since I have to convert double to float? Commented Jun 4, 2018 at 13:48
  • @Mogi - they all will give you the same resulting f value. Commented Jun 4, 2018 at 13:49
  • 2
    @sebrockm: Not necessarily; only if the original array is read-only and the program attempts to modify data pointed at by d. Commented Jun 4, 2018 at 13:54

2 Answers 2

6

They all boil down to the same thing, and the use of arrays is a red herring. You can indeed write

float f = d;

Some folk argue that a static_cast makes code more readable as it sticks out so clearly. It can also defeat warnings that some compilers might issue if a less long-winded form is used.

Naturally of course since a double is a superset of float, you might lose precision. Finally, note that for

float f1 = whatever;
double d1 = f1;
float f2 = d1;

, the C++ standard insists that f1 and f2 must be the same value.

Sign up to request clarification or add additional context in comments.

2 Comments

static_cast helps to wipe out warning message about truncating from double to float. I belive this is the main reason for using it, not readability
@Jeka: Indeed you're correct. I have all such warnings explicitly suppressed in all my builds.
0

You do have one major issue. This is not allowed:

double *d = (double*)(buffer + offset);

It violates strict aliasing and quite possibly alignment requirements. Instead you need to use memcpy:

double d;
memcpy(&d, buffer + offset, sizeof d);
float f = d;

Either of the cast alternative can be substituted for the last line, the important change is from dereferencing an pointer with incorrect type and alignment to making a bytewise copy.

1 Comment

It depends of the real type at buffer + offset BTW. (can be a double* casted to char* and restore back to double*) (Name of the function suggest it is not the case though).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.