Does it matter which operand of integer division is static_casted to obtain a float result?

Masked Man

To obtain a float result from division of two ints, we static_cast one of the operands to float, like so:

int a = 2;
int b = 3;
float c = static_cast<float>(a) / b;  // c = 0.666666
float d = a / static_cast<float>(b);  // d = 0.666666

In the above case, it shouldn't matter which operand is static_casted. However, suppose one of the operands is a compile-time constant, while the other is not, like so:

int a = foo();  // value not available at compile-time.
const int b = SOME_CONSTANT;  // compile-time constant.

Does compiler optimization make any difference to the two static_casts, as described below?

float c = static_cast<float>(a) / b;

In this case, compiler can replace b with its value, but since a isn't known, the cast can happen only at runtime.

float d = a / static_cast<float>(b);

However, in this case, compiler knows b, so it could do the casting at compile-time, and replace b directly with the float value.

In both cases, after the casting, an integer/float (or float/integer) division happens at runtime.

Is this intuition correct, or can compilers be smart enough to optimize equally well in both cases? Are there any other factors I have overlooked?

molbdnilo

No int/float or float/int divisions happen at runtime. Ever.

Since one operand is being cast - explicitly converted - to float, the other will be implicitly converted to float for the division.

Both your cases are equivalent to

static_cast<float>(a) / static_cast<float>(b);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does it matter which operand of integer division is static_casted to obtain a float result?

From Dev

integer division to float result

From Dev

integer division to float result

From Java

What is the fastest integer division supporting division by zero no matter what the result is?

From Dev

Integer division, or float multiplication?

From Dev

Integer division, or float multiplication?

From Dev

Boolean equal: 0 == a, does operand order matter?

From Dev

Numpy array integer / float division

From Dev

Numpy array integer / float division

From Dev

Java integer division result as double?

From Dev

Flask + Nginx proxy: Does it matter which one serves static files?

From Dev

Does it matter which static IP address I choose for my devices?

From Dev

Float Static Cast for Division

From Dev

python float division does not work

From Dev

Float integer division and setting variable value

From Dev

Division of Integer Parameters Returning Non Float Value

From Java

How to do integer division in javascript (Getting division answer in int not float)?

From Dev

VB Script Integer division vs Float Division Rounding up

From Dev

Julia/LLVM Efficient Division of Integer Numbers with Integer Result

From Dev

Does the order in which dynamic libraries are loaded matter?

From Dev

Does it matter which microphone is used for sound recognition?

From Dev

Does it matter which line the <br> tag is on?

From Dev

Static regex object or does it matter?

From Dev

How does Integer Division work in python?

From Dev

Does it matter which table to select FROM and which to JOIN

From Dev

Why do we separately cast to "float" in an integer division?

From Java

What is the reason for difference between integer division and float to int conversion in python?

From Dev

Rounding up integer without using float, double, or division

From Dev

What is the reason for difference between integer division and float to int conversion in python?

Related Related

  1. 1

    Does it matter which operand of integer division is static_casted to obtain a float result?

  2. 2

    integer division to float result

  3. 3

    integer division to float result

  4. 4

    What is the fastest integer division supporting division by zero no matter what the result is?

  5. 5

    Integer division, or float multiplication?

  6. 6

    Integer division, or float multiplication?

  7. 7

    Boolean equal: 0 == a, does operand order matter?

  8. 8

    Numpy array integer / float division

  9. 9

    Numpy array integer / float division

  10. 10

    Java integer division result as double?

  11. 11

    Flask + Nginx proxy: Does it matter which one serves static files?

  12. 12

    Does it matter which static IP address I choose for my devices?

  13. 13

    Float Static Cast for Division

  14. 14

    python float division does not work

  15. 15

    Float integer division and setting variable value

  16. 16

    Division of Integer Parameters Returning Non Float Value

  17. 17

    How to do integer division in javascript (Getting division answer in int not float)?

  18. 18

    VB Script Integer division vs Float Division Rounding up

  19. 19

    Julia/LLVM Efficient Division of Integer Numbers with Integer Result

  20. 20

    Does the order in which dynamic libraries are loaded matter?

  21. 21

    Does it matter which microphone is used for sound recognition?

  22. 22

    Does it matter which line the <br> tag is on?

  23. 23

    Static regex object or does it matter?

  24. 24

    How does Integer Division work in python?

  25. 25

    Does it matter which table to select FROM and which to JOIN

  26. 26

    Why do we separately cast to "float" in an integer division?

  27. 27

    What is the reason for difference between integer division and float to int conversion in python?

  28. 28

    Rounding up integer without using float, double, or division

  29. 29

    What is the reason for difference between integer division and float to int conversion in python?

HotTag

Archive