Mail Archives: djgpp/2005/05/26/05:30:39
Dear Larry,
I was thinking in what you said...actually, C/C++ is not my strong ( even if
I've already made some major complicated calculations ).
You might have a point. But, just for the fun, I made a little routine in
basic ( knowned as the worst language to work with double precision
numbers ).
CLS
ON ERROR GOTO 100
n = 0
REM Doubles
DEFDBL F, V
REM Long integer
DEFLNG I
DO UNTIL F <> 0
n = n + 1
I = 2 ^ n
V = LOG(I) / LOG(2)
F = V - INT(V)
PRINT n, I, F
LOOP
END
100 RESUME 101
101 PRINT "END CAUSED BY ERROR"
END
it stopped at n=30, 2^30, or 1073741824 - not because the fraction part
wasn't zero but because the variable I ( Long ) was overflow.
I've deal with precision numbers in astronomy applications. They are the
worst. By bad luck, the linux servers only accept compiled C, and I hate
Windows hosting. Also, I don't know if our friend wants numbers over 2^30.
Still, I have much to learn in C. And, thinking it over, you might be right.
Cheers,
Rui Fernandes
----- Original Message -----
From: "Larry Ziegenbein" <larryzie AT gmail DOT com>
To: <djgpp AT delorie DOT com>
Sent: Wednesday, May 25, 2005 9:58 PM
Subject: Re: to check given no. is power of 2 ( formatted text...)
> Hi Rui,
>
> I think when you treat a double as an int, for example comparing
> fracpart to 0 in this line --
>
> if (fracpart == 0) return 1;
>
> you will most often be disappointed. Doubles will generally have a
> bit of imprecision in the last few placeholders and throw you off
> unless you plan for it in advance. Give it a try and step through
> this code so you understand. ;-) You could round the result to the
> appropriate number of digits and recast it as an int before the
> compare maybe to make it work, but the other solutions posted are
> pretty nice looking to me!!
>
> Anyway, this code generally won't work, and it is dangerous to do
> things like this, at least for newbies like me, I avoid this like the
> plague. But I screw up in other places to make up for it.
>
> happy hacking,
> Larry
>
>
> On 5/25/05, cosmos <cosmos AT tvtel DOT pt> wrote:
> >
> > ----- Original Message -----
> > From: "cosmos" <cosmos AT tvtel DOT pt>
> > To: <djgpp AT delorie DOT com>
> > Sent: Wednesday, May 25, 2005 8:27 PM
> > Subject: Re: to check given no. is power of 2
> >
> >
> > > Hi,
> > >
> > > Mathematically, a given number "N" is a power of 2 if "log(N) /
log(2)" is
> > > an integer, right? So:
> > >
> > > #include <stdio.h>
> > > #include <math.h>
> > > double param, fractpart, intpart;
> > >
> > > param = log(N) / log(2);
> > > fractpart = modf (param , &intpart);
> > > if (fracpart == 0) return 1; else return 0;
> >
> >
> > Regards,
> >
> > Rui Fernandes
> > >
> > >
> > > ----- Original Message -----
> > > From: "Martin Ambuhl" <mambuhl AT earthlink DOT net>
> > > Newsgroups: comp.os.msdos.djgpp
> > > To: <djgpp AT delorie DOT com>
> > > Sent: Wednesday, May 25, 2005 7:39 PM
> > > Subject: Re: to check given no. is power of 2
> > >
> > >
> > > > Gerd Termathe wrote:
> > > > > "aveo" <rahul DOT h AT gmail DOT com> schrieb im Newsbeitrag
> > > > > news:1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com...
> > > > >
> > > > >>hi all
> > > > >>i need a C code that checks given no. is power of 2 or not without
> > > > >>checking any condition.
> > > > >>
> > > > >
> > > > >
> > > > > int is_power_of_2 (int N)
> > > > > {
> > > > > if ( N!=0 && (N&(N-1))==0 ) return 1; else return 0;
> > > > > }
> > > >
> > > > Why an if ... else?
> > > > Why use uppercase for non-macros?
> > > > Why use signed integers?
> > > >
> > > > inline unsigned is_power_of_2 (unsigned n)
> > > > {
> > > > return (n && !(n&(n-1)));
> > > > }
> > > >
> > >
> > >
> >
> >
> >
- Raw text -