round and abs in c++

This is a discussion on round and abs in c++ within the C++ Programming forums, part of the General Programming Boards category; looking for this equivalent functions in c++ in pascal you just write round x or abs x and outputs what ...

  1. #1
    asimos
    Guest

    round and abs in c++

    looking for this equivalent functions in c++

    in pascal you just write round x or abs x and outputs what u want

    im searching like half an hour the msdn found something for abs but its of type int abs( int n );

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb try this...

    I'm not sure bout rounding but for abs:

    #include <math.h>
    #include <iostream.h>
    #include <stdlib.h>

    int main() {

    int number = -5;

    cout << abs(number);

    system("PAUSE");
    return 0;
    }
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Unregistered
    Guest
    There is ceil() and floor(); and setprecision(); and of course truncation, but I don't know of a standard rounding function. I've seen some 3rd party versions that don't look to tough to write, so give a shot yourself and see what you come up with

  4. #4
    asimos
    Guest
    isnt that weird a whole prog. language like c++ doesnt have a
    predifined function for round

    anyways ill try something out
    hehehe

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    to round to nearest int....

    int Round( double in)
    {
    return (floor (in+0.5));
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    asimos
    Guest
    int rounding( float y,float yaxis)
    {
    double a;
    char &buffer;
    int decimal,sign;

    a = (y+10)/yaxis;
    buffer = _fcvt( a, 0 , &decimal , &sign );

    return buffer;

    };

    well after doing some research in the msdn i found this and modified and made this function
    but i get an error
    cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

  7. #7
    asimos
    Guest
    ok boys i managed thx to the last guy for the help

    int round(float y, float yaxis)
    {
    double in;
    in = (y+10)/yaxis;

    return (floor (in+0.5));

    };

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21