c - Is it needed to typecast float to double when using math.h? -


i use cosmic c compiler stm8 micro controllers , use float variables. used platform compiler not provide double types. anyway, when 1 uses doubles treated floats.

i wonder whether needed typecast float double when using e.g. fabs()? declared double fabs(double x);

i haven't found in compiler docs regarding this.

but without typecasting, compiles without warnings. way, gcc compiles without warnings when mix double float.

numeric promotion

whenever value 1 type converted value of larger similar data type, called numeric promotion (or widening, though term reserved integers). example, int can widened long, or float promoted double:

1 2 long l(64); // widen integer 64 long double d(0.12f); // promote float 0.12 double while term “numeric promotion” covers type of promotion, there 2 other terms specific meanings in c++:

integral promotion involves conversion of integer types narrower int (which includes bool, char, unsigned char, signed char, unsigned short, signed short) integer (if possible) or unsigned int. floating point promotion involves conversion of float double. integral promotion , floating point promotion used in specific cases convert smaller data types int/unsigned int or double, because data types performant perform operations on.

the important thing remember promotions safe, , no data loss result.

source:

http://www.learncpp.com/cpp-tutorial/44-implicit-type-conversion-coercion/


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -