In Steven Prata's book "C Primer Plus", there's a section on Type Conversions, wherein "The basic rules are" section has stated in rule 1:
Under K&R C, but not under current C, float is automatically converted to double.
http://www.9wy.net/onlinebook/CPrimerPlus5/ch05lev1sec5.html
Could someone explain what but not under current C
means? Are there versions of C that auto-convert and versions that don't?
I'm trying understand if I have an expression that mixes floats and doubles, can I rely on C to promote floats to doubles when it's evaluated?
The result of the multiplication of a float and an int is a float . Besides that, it will get promoted to double when passing to printf . You need a %a , %e , %f or %g format. The %d format is used to print int types.
float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.
First off, you can multiply floats. The problem you have is not the multiplication itself, but the original number you've used. Multiplication can lose some precision, but here the original number you've multiplied started with lost precision. This is actually an expected behavior.
Though both Java float vs Double is approximate types, use double if you need more precise and accurate results. Use float if you have memory constraint because it takes almost half as much space as double. If your numbers cannot fit in the range offered by float, then use double.
It must refer to the result of binary arithmetic operations of float * float
format. In the pre-standard versions of C operands of such expressions were promoted to double
and the result had double
type.
For example, here's a quote from "C Reference Manual"
If both operands are int or char, the result is int. If both are float or double, the result is double.
In C89/90 already this behavior was changed and float * float
expressions produce float
result.
- If either operand has type long double, the other operand is converted to long double
- Otherwise, if either operand is double, the other operand is converted to double.
- Otherwise, if either operand is float, the other operand is converted to float.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With