Thread: abs of float

  1. #1
    kohatian3279
    Guest

    abs of float

    how can we find the absolute of a float variable since it gives warning of data loss?

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Question

    Originally posted by vVv
    Code:
    #include <cmath>
    ...
    float foobar = -1.23;
    foobar = std::abs( foobar );
    Is abs() a member of std, I think it's not?

    And does any body know why does the compiler give that warning?
    none...

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If you include cmath, the abs function is in the std namespace. I don't see anything wrong with it.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I asked about this because when I compiled the following code( using MS VC++ 6:
    Code:
    #include <cmath>
    
    int main()
    {
    
    	float foobar = -1.23;
    	foobar = std::abs( foobar );
    	return 0;
    }
    I got the following errors and warnings:
    --------------------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)
    Thanks in advance...
    none...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Thanks alot, vVv and Prelude...
    Now it's clear...
    none...

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Alternatively you can use fabs() defined in math.h, which has been created specifically for floating point arguements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM