CPP float behavior

hi,

when i assign the updateTime float to 1/12 the animations go super fast because cpp seems to be rounding. only assigning it as 1.0/12.0 works as demonstrated. why would you need to use fractions after assigning a float?

Hello six_biomes,

The answer for the behaviour you’re experiencing has to do with how the compiler interprets what expression results in an integer and what is a float.

To start (and as a quick reminder):

  • An integer is a whole number, with no fractions or decimals (1, 2, 3, 4… etc)
  • A float (or floating point) number is a number that can be expressed with a fraction or decimals (1.1, 4.23, 0.9821, 3.14, etc…)

A number that is an integer and can stored as an integer (1 becomes 1.0) but for the reverse, the fraction/decimal gets dropped (1.34 becomes 1).

When you assign a value to a variable that is of type float that is of a different numerical type, the compiler will do a conversion from that type into a float (this is also were a narrowing conversion warning can come from, but that’s for another day). Any mathematical expressions get evaluated for the type that the expression evaluates to first, before attempting conversion.

In this case. 1.0 / 12.0 works out as float / float, which gets you 0.83333333~, and the compiler has no problem with assigning that float value into the variable that is also a float.

However, 1 / 12 works out as integer / integer. So even if the result should be 0.83333333~, the compiler believes the result should be a number of type integer, which evaluates to 0.

thanks for the long reply but i was mainly curious about the last part. why does c++ evaluate a fraction inside a float as an integer, especially if it calls itself a “strongly typed language”. you said because both components of the expression are an integer by themselves. which would mean the compiler retanslates the numbers type before evaluating an expression? which would make me assume that i can make it force evaluate in other ways as well (e.g. with parenthesis). its still just weird, if i tell you its a float, why you decide to make it an int ;-).

Because you can assign an int to a float, the compiler just makes a conversion to do so (this is likely to save time doing implicit conversions because programmers tend to not like wasting time with repetitive tasks).

The drawback to that is now the compiler doesn’t know your intent. But as you alluded to there are ways to get what you want, which is especially useful if you’re dealing with variables and not with hard-coded values like with updateTime.

Let’s assume three variables, one of type float and the other two of type int. Now let’s try the same assignment experiment.

floatvariable = int_variable_one / int_variable_two

We can’t convert a variable of type int into a float like we can with hard-coded values (like with 1 to 1.0), instead we’ll use a cast to inform the compiler we want a different type.

floatvariable = (float)int_variable_one / (float)int_variable_two

This type of cast is referred to as a “c-style” cast, as it comes from the C language that C++ is built on top of. This works for pre-defined data types but may not always work for custom data types, I’ll leave the research of the other cast methods as a challenge for you.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms