In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks. 11 3120
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
On Apr 23, 5:15 pm, xz <zhang.xi...@gmail.comwrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Well, that certainly sheds some light on things. "treat it as" means
"do different things"....hmmm
Pretty hard to help you when you won't describe what you are trying to
do.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
Still the same principle:
if( remainder - tolerance < 0.0 )
{
// Do whatever you mean by treat as int and round down if you wish
}
else if( remainder + tolerance 1.0 )
{
// Do whatever you mean by treat as int and round up if you wish
}
Are you looking for a "algorithm" like this then?
bool isNearInt( double value )
{
const double epsilon = 0.0001;
const double diff = value - floor( value );
return ( diff <= epsilon || diff >= (1.0 - epsilon) );
}
which should at least answer the within epsilon of an integer value.
xz wrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
>On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
>>In a program, if a double is within 0.0001 from an int, then I treat it as int. For example, 10.00005 will be treated as int, while 10.001 is not. I am looking for an algorithm to implement this checking. Anybody gives an idea? Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the double and compare it to .0001 or any variable amount you wish. Getting the quotient and remainder after dividing by 1 comes to mind. I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
On Apr 23, 5:35 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 5:15 pm, xz <zhang.xi...@gmail.comwrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Well, that certainly sheds some light on things. "treat it as" means
"do different things"....hmmm
Pretty hard to help you when you won't describe what you are trying to
do.
Sorry about not having expressed it clearly and thanks for your help.
>
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
Still the same principle:
if( remainder - tolerance < 0.0 )
{
// Do whatever you mean by treat as int and round down if you wish}
else if( remainder + tolerance 1.0 )
{
// Do whatever you mean by treat as int and round up if you wish
}
However, this is like a brutal force way that I actually thought
about.
I am wondering if there is any smarter way to do that? Like finishing
the job in a relatively simple expression?
Still appreciate a lot for your help.
On Apr 23, 5:56 pm, xz <zhang.xi...@gmail.comwrote:
In a program, if a double is within 0.0001 from an int, then I treat
it as int. For example, 10.00005 will be treated as int, while 10.001
is not.
I am looking for an algorithm to implement this checking. Anybody
gives an idea?
Thanks.
Something like this? but see also http://www.parashift.com/c++-faq-lit...html#faq-29.17
bool close_to_int (double d, double tolerance) {
double cdiff = std::fabs (std::ceil (d) - d);
double fdiff = std::fabs (std::floor (d) - d);
if (cdiff < fdiff)
return cdiff <= tolerance;
return fdiff <= tolerance;
}
xz wrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
>On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
>>In a program, if a double is within 0.0001 from an int, then I treat it as int. For example, 10.00005 will be treated as int, while 10.001 is not. I am looking for an algorithm to implement this checking. Anybody gives an idea? Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the double and compare it to .0001 or any variable amount you wish. Getting the quotient and remainder after dividing by 1 comes to mind. I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
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) {
int i = near;
// use i or near
}
else
{
// use d
}
--
Thomas
On Apr 23, 5:48 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
xz wrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
>In a program, if a double is within 0.0001 from an int, then I treat it as int. For example, 10.00005 will be treated as int, while 10.001 is not. I am looking for an algorithm to implement this checking. Anybody gives an idea? Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
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) {
int i = near;
// use i or near}
else
{
// use d
}
--
Thomas
+0.5!
That's smart! Exactly what I wanted!
Thanks a lot!
On Apr 23, 5:48 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
xz wrote:
On Apr 23, 5:05 pm, Christopher <cp...@austin.rr.comwrote:
On Apr 23, 4:56 pm, xz <zhang.xi...@gmail.comwrote:
>In a program, if a double is within 0.0001 from an int, then I treat it as int. For example, 10.00005 will be treated as int, while 10.001 is not. I am looking for an algorithm to implement this checking. Anybody gives an idea? Thanks.
Depends what you mean by "treat it as"
Surely the math is simple enough to grab to decimal portion of the
double and compare it to .0001 or any variable amount you wish.
Getting the quotient and remainder after dividing by 1 comes to mind.
I doubt I'd even call it an algorithm...
By "treat it as" I mean I will do different things for the doubles
near int and for the doubles not near int.
Your trick (if you don't wanna call it algorithm) does not really work
for my case because I wanna 9.99999 to be "near int" however its
decimal portion is 0.99999.
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?
int i = near;
// use i or near}
else
{
// use d
}
--
Thomas
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
On Apr 23, 9:59*pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ernie |
last post by:
Hi,
I am learning swig to use C codes from Python.
Here is the swig file mvf.i:
%module mvf
%include "carrays.i"
%array_class(double, doubleArray);
%typemap(out) double, float "$result =...
|
by: Praveen |
last post by:
Hi All,
I'm new to DB2, I have installed DB2 UDB V8 on Solaris machine. And i
tried to create a stored proc.But, gives the following error..
-- LOG FILE P2093405.log FOR PROCEDURE...
|
by: Fred Ma |
last post by:
I'm using the expression "int a = ceil( SomeDouble )".
The man page says that ceil returns the smallest
integer that is not less than SomeDouble, represented
as a double. However, my...
|
by: Jason |
last post by:
I have created a 2d isometric game map using tiles and now I'm trying to
move around my map..when i go near the edge of the map I want to redraw the
map to show new parts of the map however the...
|
by: Siv |
last post by:
Hi,
I have a ListView control in a Windows application, currently single
clicking a customer name in this list, selects the customer and displays
their details in text boxes to the right of the...
|
by: Siv |
last post by:
Hi,
I posted earlier in the week and no-one responded so i thought I would try
again. My question is:
I have a ListView control in a Windows application, currently single
clicking a customer...
|
by: ma740988 |
last post by:
template <class T> inline bool isEqual( const T& a, const T& b,
const T epsilon = std::numeric_limits<T>::epsilon() )
{
const T diff = a - b;
return ( diff <= epsilon ) && ( diff >= -epsilon );...
|
by: shuisheng |
last post by:
Dear All,
Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always...
|
by: Sells, Fred |
last post by:
I'm using python 2.4 under linux (centos 5.1).
I need to pass an array of doubles to a c function
but am getting an error, shown near the bottom of
this post....
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |