Why does dividing a int with a float result in a float?

treeboy3

Question why does this happen? Is this just a C language thing?

I'm following the cs50 course.

 #include <stdio.h>
 
 int main(void)
 {
     int testInt = 5;
     printf("%f", testInt / 4.0);      
 }

Output is 1.250000 -- float value

Jonathon Reinhart

Is this just a C language thing?

The answer is "because that's how the C language defines the operation."

It is common in many languages to promote an integer to a floating point before doing an operation with another floating point value.

If it didn't work this way, there would be many accidental loss-of-precision (or loss-of-information) bugs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does dividing a float by an integer return 0.0?

Does dividing float by int always give float? C++

Why does "Int" exist, but not "Float"?

Why does dividing by zero for Float not raise an exception in Ruby?

Why (int)==(float) always compiles to (float)==(float)

Why is there int but not float in Go?

Does assigning an int or a float to an interface result internally in a block allocation?

Why does Int(Float(Int.max)) give me an error?

Why does int(float(str)) work but int(str) not work

Result of ternary operator on `int` and `float`

Why is the sum of an int and a float an int?

Why does casting to float produce correct result in java?

Why does a FLOAT give me a more accurate result than a DECIMAL?

Why does the printing of this float number result in the wrong answer in c

Why does C++ promote an int to a float when a float cannot represent all int values?

Why is an addition of a float and int results a float?

Python: dividing a float by a variable

Dividing a character with a Float in Ada

Golang's type system behaviour - dividing an int by an implicit float

Python why does issubclass(int, float) return False

Why does a type hint `float` accept `int` while it is not even a subclass?

Why does a cast from int to float round the value?

Why does std::pow of a float and int invoke this overload?

Why does float argument fit to int function parameter?

After casting float to float/int returned different result

Why does float('inf') == float('inf') return True, but float('inf') - float('inf') == float('inf') returns False?

Why is it faster to perform float by float matrix multiplication compared to int by int?

Datetime int float, why we can go to float but not to int

Wrong addition result when adding float and int