C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to tell if a number has decimal places different than 0
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gaijinco
Guest





PostPosted: Mon Sep 26, 2005 8:07 pm    Post subject: How to tell if a number has decimal places different than 0 Reply with quote



Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?

Back to top
Emmanuel Delahaye
Guest





PostPosted: Mon Sep 26, 2005 8:10 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote



Gaijinco wrote on 26/09/05 :
Quote:
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?

fmod()

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Back to top
John Harrison
Guest





PostPosted: Mon Sep 26, 2005 9:05 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote



Gaijinco wrote:
Quote:
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?


Language-specific? What do you mean by that?

Your test is not perfect because it will fail when square is bigger than
the biggest int. This test always works (within the limitations of
floating point accuracy).

#include <math>

double square = sqrt(number);
if (floor(square) == square)
// number is a perfect square
else
// number is not a perfect square

john

Back to top
Eric Sosman
Guest





PostPosted: Mon Sep 26, 2005 9:12 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote



Gaijinco wrote On 09/26/05 16:07,:
Quote:
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square

.... and the test is a tautology.

Quote:
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?

The code you've shown will (if it doesn't invoke
undefined behavior) declare that every number is a
perfect square: 1, 2, 3.14, and even -42.

To test whether a floating-point number is an
integer with no fractional part, you could try

if ((int)fpn == fpn) ...

This runs into trouble when the magnitude of fpn
is large, so large that its value is outside the range
of numbers representable as `int'.

As an improvement you might try

if (fmod(fpn, 1.0) == 0.0) ...

This is still vulnerable to the "graininess" of
floating-point numbers, which are not mathematical real
numbers with infinite precision.

You cannot usually expect sqrt(fpn) to be the exact square
root of fpn. sqrt(fpn) will be very close to the exact root,
but will (in general) be just a little bit different from the
true value. There could be several different fpn values for
which sqrt(fpn) would deliver exactly the same slightly wrong
answer: both sqrt(4.0) and sqrt(4.0 + tiny_number) might
produce 2.0 as an answer. If you decide that a number is a
perfect square if its computed square root turns out to be an
integer, you will erroneously conclude that 4.0+tiny_number is
a perfect square.

A possibly more thorough test might compute the square
root, test whether it's an integer, and then test whether
its square equals the original number:

double root = sqrt(number);
if (fmod(root, 1.0) == 0.0 && root * root == number)

.... but even this may have some problems. I am always uneasy
when comparing floating-point quantities for exact equality,
mostly because fpn's are usually regarded as approximations
to begin with. You usually need an "approximately equal"
test of some kind, and such a test isn't well suited to the
purely yet/no nature of perfect squaredom.

For "number-theoretic" calculations you'll usually be much
better off using integers of some flavor. If the numbers grow
large you may need to resort to a "bignum" package; several
are available.

--
[email]Eric.Sosman (AT) sun (DOT) com[/email]



Back to top
Mark McIntyre
Guest





PostPosted: Mon Sep 26, 2005 9:18 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

On Mon, 26 Sep 2005 21:05:09 GMT, in comp.lang.c , John Harrison
<john_andronicus (AT) hotmail (DOT) com> wrote:

(of testing to see if a float is a perfect square)

Quote:
This test always works (within the limitations of
floating point accuracy).

I'd not be too sure of that. Remember floating point is not an exact
representation.

Quote:
double square = sqrt(number);
if (floor(square) == square)
// number is a perfect square
else
// number is not a perfect square
--

Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Back to top
John Harrison
Guest





PostPosted: Mon Sep 26, 2005 9:20 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Mark McIntyre wrote:
Quote:
On Mon, 26 Sep 2005 21:05:09 GMT, in comp.lang.c , John Harrison
[email]john_andronicus (AT) hotmail (DOT) com[/email]> wrote:

(of testing to see if a float is a perfect square)


This test always works (within the limitations of
floating point accuracy).


I'd not be too sure of that. Remember floating point is not an exact
representation.

That's why I said 'within the limitations of floating point accuracy'.
My test always checks if a floating point number is integral. Obviously
an integral return from sqrt does not necessarily mean the sqrt
parameter was a perfect square.

john

Back to top
Eric Sosman
Guest





PostPosted: Mon Sep 26, 2005 9:32 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote



John Harrison wrote On 09/26/05 17:20,:
Quote:
Mark McIntyre wrote:

On Mon, 26 Sep 2005 21:05:09 GMT, in comp.lang.c , John Harrison
[email]john_andronicus (AT) hotmail (DOT) com[/email]> wrote:

(of testing to see if a float is a perfect square)



This test always works (within the limitations of
floating point accuracy).


I'd not be too sure of that. Remember floating point is not an exact
representation.


That's why I said 'within the limitations of floating point accuracy'.
My test always checks if a floating point number is integral. Obviously
an integral return from sqrt does not necessarily mean the sqrt
parameter was a perfect square.

On the machine in front of me right now, sqrt(1.0)
and sqrt(1.0000000000000002) both give 1.0 as the root.

--
[email]Eric.Sosman (AT) sun (DOT) com[/email]


Back to top
Martin Ambuhl
Guest





PostPosted: Mon Sep 26, 2005 9:37 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Gaijinco wrote:
Quote:
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?


using modf, you can test either the integral part or the fractional
part. Look askance at any solution that involves ints.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

int main(void)
{
double x, fp, ip;
int loop, cnt;
srand(time(0));
for (loop = cnt = 0; cnt < 10; loop++) {
x = (int) (100. * rand() / (1. + RAND_MAX)) / 10.;
fp = modf(x, &ip);
if (ip != x || fp)
continue;
printf("%4d: x = %g, fractional part (fp) = %g,"
"integer part (ip) = %gn"
" (ip %s x, fp %s 0)n", loop, x, fp, ip,
(ip == x) ? "==" : "!=", (fp == 0) ? "==" : "!=");
cnt++;
}
return 0;
}


1: x = 8, fractional part (fp) = 0,integer part (ip) = 8
(ip == x, fp == 0)
36: x = 0, fractional part (fp) = 0,integer part (ip) = 0
(ip == x, fp == 0)
63: x = 6, fractional part (fp) = 0,integer part (ip) = 6
(ip == x, fp == 0)
70: x = 1, fractional part (fp) = 0,integer part (ip) = 1
(ip == x, fp == 0)
79: x = 5, fractional part (fp) = 0,integer part (ip) = 5
(ip == x, fp == 0)
87: x = 5, fractional part (fp) = 0,integer part (ip) = 5
(ip == x, fp == 0)
128: x = 4, fractional part (fp) = 0,integer part (ip) = 4
(ip == x, fp == 0)
147: x = 1, fractional part (fp) = 0,integer part (ip) = 1
(ip == x, fp == 0)
148: x = 8, fractional part (fp) = 0,integer part (ip) = 8
(ip == x, fp == 0)
162: x = 1, fractional part (fp) = 0,integer part (ip) = 1
(ip == x, fp == 0)

Back to top
John Harrison
Guest





PostPosted: Mon Sep 26, 2005 9:42 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Quote:

On the machine in front of me right now, sqrt(1.0)
and sqrt(1.0000000000000002) both give 1.0 as the root.


Am I missing something? What point are you making?

john

Back to top
Keith Thompson
Guest





PostPosted: Mon Sep 26, 2005 10:15 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

John Harrison <john_andronicus (AT) hotmail (DOT) com> writes:
[...]
Quote:
#include <math

double square = sqrt(number);
if (floor(square) == square)
// number is a perfect square
else
// number is not a perfect square

I suggest that "square" is a really bad name for a variable that hold
the result of a call to sqrt().

--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Back to top
Pete Becker
Guest





PostPosted: Mon Sep 26, 2005 11:31 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Mark McIntyre wrote:
Quote:

I'd not be too sure of that. Remember floating point is not an exact
representation.


Floating point is exact. Unlike real numbers, it is not continuous.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Back to top
E. Robert Tisdale
Guest





PostPosted: Mon Sep 26, 2005 11:43 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Pete Becker wrote:

Quote:
Mark McIntyre wrote:

I'd not be too sure of that.
Remember floating point is not an exact representation.

Floating point is exact. Unlike real numbers, it is not continuous.

In general, *finite precision* floating-point arithmetic is *inexact*.
The set real numbers includes irrational numbers which have *no*
finite precision digital representation in this universe --
they exist only in the minds of mathematicians.

Back to top
Keith Thompson
Guest





PostPosted: Tue Sep 27, 2005 12:21 am    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

Pete Becker <petebecker (AT) acm (DOT) org> writes:
Quote:
Mark McIntyre wrote:
I'd not be too sure of that. Remember floating point is not an exact
representation.

Floating point is exact. Unlike real numbers, it is not continuous.

It depends on how you look at it. A given floating-point
representation can be viewed either as an exact value, or as an
inexact approximation of any of the infinitely many real numbers close
to the exact represented value. The latter, though arguably
incorrect, is probably the more common interpretation, especially
given things like:

double one_third = 1.0/3.0;

Integers are usually considered to be exact because they typically
aren't thought of as being approximations of nearby numbers; 42 really
is 42, not an approximation of 42.0625.

--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Back to top
Dik T. Winter
Guest





PostPosted: Tue Sep 27, 2005 1:19 am    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

In article <dha13e$ko8$1 (AT) nntp1 (DOT) jpl.nasa.gov> [email]E.Robert.Tisdale (AT) jpl (DOT) nasa.gov[/email] writes:
Quote:
Pete Becker wrote:
Mark McIntyre wrote:

I'd not be too sure of that.
Remember floating point is not an exact representation.

Floating point is exact. Unlike real numbers, it is not continuous.

In general, *finite precision* floating-point arithmetic is *inexact*.

I think you misunderstand Pete Beckers meaning. Assuming IEEE, given
two operands and an operation, it is precisely predictable what the
result is. But I understand you are pretty good at misunderstanding.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

Back to top
Eric Sosman
Guest





PostPosted: Tue Sep 27, 2005 2:10 pm    Post subject: Re: How to tell if a number has decimal places different tha Reply with quote

John Harrison wrote:
Quote:

On the machine in front of me right now, sqrt(1.0)
and sqrt(1.0000000000000002) both give 1.0 as the root.

Am I missing something? What point are you making?

Just giving a concrete example of the point you made
(and snipped):

Quote:
[...] Obviously
an integral return from sqrt does not necessarily mean the sqrt
parameter was a perfect square.

--
Eric Sosman
[email]esosman (AT) acm-dot-org (DOT) inva[/email]lid

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.