delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2005/05/26/05:47:50

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Received-SPF: none (free.tvtel.pt: domain of cosmos AT tvtel DOT pt does not designate permitted sender hosts)
Message-ID: <00f201c561d7$e775b100$b40a6652@rui>
From: "cosmos" <cosmos AT tvtel DOT pt>
To: <djgpp AT delorie DOT com>
References: <1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com> <d71i5h$t6h$1 AT nntp DOT fujitsu-siemens DOT com> <3E3le.7676$M36 DOT 381 AT newsread1 DOT news DOT atl DOT earthlink DOT net> <00bd01c5615f$ce6a7d80$b40a6652 AT rui> <00e301c56166$b7edca60$b40a6652 AT rui> <260f63050525135825d3128c AT mail DOT gmail DOT com>
Subject: ADDENDA: Re: to check given no. is power of 2 ( formatted text...)
Date: Thu, 26 May 2005 10:47:12 +0100
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
X-Virus-Scanned: ClamAV version 0.84, clamav-milter version 0.84e on free.tvtel.pt
X-Virus-Status: Clean
Reply-To: djgpp AT delorie DOT com

Hi again,

With Visual Basic, taking I as currency ( much bigger that long ), the
calculation goes to 2^49, again by overflow.

Private Sub Form_Load()

On Error GoTo 100
n = 0
Rem Doubles
Dim F, V As Double
Rem Long integer
Dim I As Currency

Do Until F <> 0
n = n + 1
I = 2 ^ n
V = Log(I) / Log(2)
F = V - Int(V)
Text1 = Text1 + Str(n) + Str(I) + Str(F) + vbCrLf
Loop

Stop

100 Resume 101
101 Text1 = Text1 + "Overflow"
Stop

End Sub


Results:

I, 2^I, 2^I - int(2^I)
 1 2 0
 2 4 0
 3 8 0
 4 16 0
 5 32 0
 6 64 0
 7 128 0
 8 256 0
 9 512 0
 10 1024 0
 11 2048 0
 12 4096 0
 13 8192 0
 14 16384 0
 15 32768 0
 16 65536 0
 17 131072 0
 18 262144 0
 19 524288 0
 20 1048576 0
 21 2097152 0
 22 4194304 0
 23 8388608 0
 24 16777216 0
 25 33554432 0
 26 67108864 0
 27 134217728 0
 28 268435456 0
 29 536870912 0
 30 1073741824 0
 31 2147483648 0
 32 4294967296 0
 33 8589934592 0
 34 17179869184 0
 35 34359738368 0
 36 68719476736 0
 37 137438953472 0
 38 274877906944 0
 39 549755813888 0
 40 1099511627776 0
 41 2199023255552 0
 42 4398046511104 0
 43 8796093022208 0
 44 17592186044416 0
 45 35184372088832 0
 46 70368744177664 0
 47 140737488355328 0
 48 281474976710656 0
 49 562949953421312 0
Overflow

Not bad for old plain basic...

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019