how can we find the absolute of a float variable since it gives warning of data loss?
how can we find the absolute of a float variable since it gives warning of data loss?
Is abs() a member of std, I think it's not?Originally posted by vVv
Code:#include <cmath> ... float foobar = -1.23; foobar = std::abs( foobar );
And does any body know why does the compiler give that warning?
none...
If you include cmath, the abs function is in the std namespace. I don't see anything wrong with it.
I asked about this because when I compiled the following code( using MS VC++ 6:
I got the following errors and warnings:Code:#include <cmath> int main() { float foobar = -1.23; foobar = std::abs( foobar ); return 0; }
Thanks in advance...--------------------Configuration: Cpp2 - Win32 Debug--------------------
Compiling...
Cpp2.cpp
C:\Documents and Settings\Ammar\My Documents\C++\Cpp2.cpp(6) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\Documents and Settings\Ammar\My Documents\C++\Cpp2.cpp(7) : error C2653: 'std' : is not a class or namespace name
C:\Documents and Settings\Ammar\My Documents\C++\Cpp2.cpp(7) : warning C4244: 'argument' : conversion from 'float' to 'int', possible loss of data
C:\Documents and Settings\Ammar\My Documents\C++\Cpp2.cpp(7) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
Error executing cl.exe.
Cpp2.exe - 1 error(s), 3 warning(s)
none...
>using MS VC++ 6
That's your problem, VC++ 6 doesn't conform to the latest standard properly.
-Prelude
My best code is written with the delete key.
Thanks alot, vVv and Prelude...
Now it's clear...
none...
The abs( ) function takes an integer number and returns an integer value. You want to use the function fabs( ) which takes a double and returns a double.
Alternatively you can use fabs() defined in math.h, which has been created specifically for floating point arguements.