abs(double) returns int in VC++ not double

abs(double) returns int in VC++ not double

Post by Ron Natali » Wed, 15 Nov 2000 04:00:00




> I experienced a * waste of time identifying a bug that ended up being caused
> by my expectation that abs(double) in <cmath> would return a double in VC++ just
> like it does under g++ - silly me. >8^(

> Is there any reason why this difference exists?? eg. "abs" for int and "fabs" for
> float.

> Should I expect any similar beasties to crawl out of VC++.

In C, there is a separate abs() for int and fabs() for DOUBLE (not float).

In C++ the standard adds overloads for float and double for abs and float for
fabs (because it can I guess).

 
 
 

abs(double) returns int in VC++ not double

Post by Karl Heinz Buchegge » Wed, 15 Nov 2000 04:00:00




> > I experienced a * waste of time identifying a bug that ended up being caused
> > by my expectation that abs(double) in <cmath> would return a double in VC++ just
> > like it does under g++ - silly me. >8^(

> > Is there any reason why this difference exists?? eg. "abs" for int and "fabs" for
> > float.

> > Should I expect any similar beasties to crawl out of VC++.

> In C, there is a separate abs() for int and fabs() for DOUBLE (not float).

> In C++ the standard adds overloads for float and double for abs and float for
> fabs (because it can I guess).

If you are right (and I don't doubt that), then this is another point
where the MS compiler doesn't conform to the standard.

#include <iostream>
#include <cmath>

int main()
{
  double d_val = -3.1415;
  float  f_val = -3.1415f;
  int    i_val = -3;

  std::cout << "double " << abs( d_val ) << std::endl;
  std::cout << "float  " << abs( f_val ) << std::endl;
  std::cout << "int    " << abs( i_val ) << std::endl;

  return 0;

Quote:}

produces:

double 3
float  3
int    3

looking through the header files, I found (in math.h, which get's
included from cmath):

....
#ifndef _MSC_EXTENSIONS

inline long __cdecl abs(long _X)
        {return (labs(_X)); }
inline double __cdecl abs(double _X)
        {return (fabs(_X)); }
....

Searching through the docu for _MSC_EXTENSIONS

_MSC_EXTENSIONS          This macro is defined when compiling with the
                         /Ze compiler option (the default).
                         Its value, when defined, is 1.

Unfortunatly I found no way of getting rid of /Ze, since this flag seems
to be not used in the compiler switches any more :-), and 'Disabling
language extensions' was of no help with respect to this.

--
Karl Heinz Buchegger


 
 
 

abs(double) returns int in VC++ not double

Post by Jord » Thu, 16 Nov 2000 00:33:17


I experienced a * waste of time identifying a bug that ended up being caused
by my expectation that abs(double) in <cmath> would return a double in VC++ just
like it does under g++ - silly me. >8^(

Is there any reason why this difference exists?? eg. "abs" for int and "fabs" for
float.

Should I expect any similar beasties to crawl out of VC++.

Regards,

Jord

 
 
 

1. overloading: double abs(double)

I found an inconstancy between the library files in
the three compilers I use and I want to know what is
the preferred way.

The code has lots of fabs() calls being passed
doubles.  Occasionally there is an abs() that is
also being passed a double.

On compiler Si the file math.h overloaded abs() with
  double abs(double x) {return fabs(x);};
(There may have been an inline too).  
This ends up doing what the programmer intended.

On compiler GNU the file math.h does not have the
overload so the double is truncated to an integer
before being passed to abs().  This is really bad
since the value is an angle in radians ranging
from minus pi to plus pi.

Compiler MS also is missing the overload and would
demoted to an integer.

I suspect that math.h has not been modified much since
its origin in C and the overloads that would allow
ignoring the types have not been added.  

Should we add these overloads?  

Should we declare the overload, but not include a
definition so the linker will bomb and the programmer
has to change to fabs()?

Should we use more code inspections and walkthroughts?

PS: Catching this problem was the result of boredom that
led me to spend an hour watching the warning messages
coming out of the compiler.

Thanks in advance

Richard Kaiser


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. DOS network application

3. Double click vs. double click

4. Netmeeting will not load :-(

5. round(double) function listed in my standard c/c++ ref but not in vc++6?

6. lame delegation

7. conversion of vector<double> to double*

8. Problem with Jaz Drive

9. What is: double (*cosine) (double);

10. declaration order: double& Matrix::operator() vs double Matrix::operator()

11. 16 byte data type (double double?)

12. How to convert foo<double> to foo<const double>?

13. Memory Allocation : double *A, double **B