Getting double result from abs (double) instead of int

I need to calculate the absolute value of the difference of two double values โ€‹โ€‹and get the result of double . Instead, I get int .

 #include <typeinfo> // ... printf( "a:%sb:%s delta:%s abs:%s\n", typeid(a).name(), typeid(b).name(), typeid(a - b).name(), typeid(abs(a - b)).name() ); // Prints: a:db:d delta:d abs:i 

If the result of the subtraction is already double, why abs does not use the signature double abs (double x); ? Indeed, how can he return the whole? Most importantly, how to make it return a double ?

If that matters, a and b are actually myData.m_lat and otherData.latitude() .

+5
source share
2 answers

To avoid collisions with inadvertently imported C standard library headers, use std::abs . This is a C ++ version and is heavily overloaded, as you already know.

Otherwise, use fabs from the C standard library.

+10
source

Prior to C ++, 17 abs for floating points and for integrals were defined in different headers ( cmath and cstdlib respectively).

+2
source

Source: https://habr.com/ru/post/1257406/


All Articles