What's up with rounding in cmath?!

T

The Cool Giraffe

I have trouble making the following line compile.

int a = round (2.6);



I have, of course, included <cmath> and i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: 'round': identifier not found

and that's the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that's just wrong, so wrong...

What can be the reason? The remedy?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I have trouble making the following line compile.

int a = round (2.6);



I have, of course, included <cmath> and i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: 'round': identifier not found

and that's the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that's just wrong, so wrong...

What can be the reason? The remedy?

A quick look in the standard gives that answer that there is no round()
function. The remedy is to get a C99 compliant library and include
<math.h> (round() was included first in C99).
 
S

Salt_Peter

I have trouble making the following line compile.

int a = round (2.6);

I have, of course, included <cmath> and i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: 'round': identifier not found

and that's the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that's just wrong, so wrong...

What can be the reason? The remedy?

round() doesn't return an int, although thats not your problem.

#include <iostream>
#include <cmath>

int main()
{
double result = round( 2.6 );
std::cout << "result = " << result << std::endl;
}

if you really need an int, use a static_cast< int >( )
 
I

Ian Collins

Erik said:
A quick look in the standard gives that answer that there is no round()
function. The remedy is to get a C99 compliant library and include
<math.h> (round() was included first in C99).
You might be able to get away with just changing the header to <math.h>
if your compiler's C mode uses the C99 library.
 
T

The Cool Giraffe

Erik Wikström wrote/skrev/kaita/popisal/schreibt :
A quick look in the standard gives that answer that there is no
round() function. The remedy is to get a C99 compliant library and
include <math.h> (round() was included first in C99).


I find that astonishing... Well, i know how to work around
it so the question was purely academic. Thanks to all of
you. It's appreciated.
 
I

Ian Collins

The said:
Erik Wikström wrote/skrev/kaita/popisal/schreibt :


I find that astonishing... Well, i know how to work around
it so the question was purely academic. Thanks to all of
you. It's appreciated.
Why is it astonishing? The round family of functions are new in C99, so
not part of the standard C++ library.
 
T

The Cool Giraffe

Ian Collins wrote/skrev/kaita/popisal/schreibt :
Why is it astonishing? The round family of functions are new in C99,
so not part of the standard C++ library.


Astonishing, or at least surprising. Why? Well, intuitively speaking,
i'd expect a routine for _rounding_ to be there, if there's one for
_flooring_ and _ceiling_. I guess, in my view, they are the three
musketeers going together.

Of course it's not impossible but i'd be surprised too if a math
package would include sine but not cosine... I hope that answered
your question.
 
P

P.J. Plauger

Ian Collins wrote/skrev/kaita/popisal/schreibt :


Astonishing, or at least surprising. Why? Well, intuitively speaking,
i'd expect a routine for _rounding_ to be there, if there's one for
_flooring_ and _ceiling_. I guess, in my view, they are the three
musketeers going together.

Of course it's not impossible but i'd be surprised too if a math
package would include sine but not cosine... I hope that answered
your question.

round is required by TR1, and is included in our complete version
available at our web site. It has also been voted into C++0X, so in
a few years it'll be a required part of Standard C++.

And BTW, round isn't as easy to implement as you might think,
particularly if you want to honor the current floating-point rounding
mode.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
A

ajk

round is required by TR1, and is included in our complete version
available at our web site. It has also been voted into C++0X, so in
a few years it'll be a required part of Standard C++.

And BTW, round isn't as easy to implement as you might think,
particularly if you want to honor the current floating-point rounding
mode.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

*when* will actually the new C++ standard come out ? I have heard a
lot of talk about it but haven't seen any concrete dates/years.

br/ajk
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

*when* will actually the new C++ standard come out ? I have heard a
lot of talk about it but haven't seen any concrete dates/years.

As I understand they (the committee) are aiming at 2009, which means
that they'll have to have a document ready to present to ISO at the end
of this year.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top