Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to check if a double is near an int?

Reply
Thread Tools

How to check if a double is near an int?

 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      04-24-2008
xz wrote:
>> Given:
>> #include <cmath>
>> const double eps = 0.0001; // your treshold
>> double d = 10.00005; // your number
>>
>> The algorithm would be:
>> 1. Round d to nearest integer.
>> 2. Check if nearest integer is within treshold.
>>
>> double near = std::floor(d+0.5);
>> if (std::abs(d - near) < eps) {

>
> I guess you meant fabs(...), right?


No. std::abs is overloaded for built-in types. fabs() has the signature:
double fabs(double);

So in this case you can use both.

--
Thomas
 
Reply With Quote
 
 
 
 
xz
Guest
Posts: n/a
 
      04-24-2008
On Apr 23, 9:59*pm, "Thomas J. Gritzan" <(E-Mail Removed)>
wrote:
> xz wrote:
> >> Given:
> >> #include <cmath>
> >> const double eps = 0.0001; // your treshold
> >> double d = 10.00005; // your number

>
> >> The algorithm would be:
> >> * *1. Round d to nearest integer.
> >> * *2. Check if nearest integer is within treshold.

>
> >> double near = std::floor(d+0.5);
> >> if (std::abs(d - near) < eps) {

>
> > I guess you meant fabs(...), right?

>
> No. std::abs is overloaded for built-in types. fabs() has the signature:
> * *double fabs(double);
>
> So in this case you can use both.


Thanks!

>
> --
> Thomas


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
ctime double double check aisling.cronin@gmail.com C Programming 11 03-09-2007 10:39 PM
from List <double> to double[] Web learner ASP .Net 3 04-26-2006 05:26 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM



Advertisments