Qbasicnews.com
February 23, 2020, 07:16:59 AM
 Pages: 1 [2] 3
 Author Topic: HELP: Why does this not work?  (Read 8956 times)
Agamemnus
x/ \z

Posts: 3491

 « Reply #15 on: July 14, 2003, 06:26:34 PM »

Glenn did. LOG(n) converts N to a float, which iis inaccurate.
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Sterling Christensen
Na_th_an

Posts: 1328

 « Reply #16 on: July 14, 2003, 06:54:57 PM »

I don't see what the problem is with Glenn's original idea of using CINT instead of INT. It works perfectly for me.
Code:

INPUT n%  ' notice that n% is an integer

' 2^(a positive integer) must produce an integer, and the variable
'    we're comparing it to is also an integer (n%), so we can assume
'    there will be no floating point error in comparison here:
IF 2 ^ CINT(LOG(n%) / LOG(2)) = n% THEN PRINT n%;"is a power of 2"

I tried every power of 2 from 1 to 16384 and it correctly identified each as a power of 2. Other numbers were ignored.
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #17 on: July 14, 2003, 07:12:28 PM »

You're right, Aga, Glenn's explanation regarding the fact that LOG gives a floating point result, basically answers my question.

However, if for n=128 the result of log(128)/log(2) is slightly less than 7, then why if you do:
a%=log(128)/log(2)
does it convert this result which is slightly less than 7, to a 7 which it puts into a%?

In other words, this result which is slightly less than 7 is treated as a 7 in one case, and not a 7 when I do all on one line.
THIS IS WHAT BUGS ME!

Somehow Basic is deciding how close a number has to be for it to ROUND it to an integer.
*****
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #18 on: July 14, 2003, 07:44:41 PM »

I understand you now.

Code:

CLS
a# = LOG(128) / LOG(2): PRINT INT(a#)      'why doesn't this round down?
IF INT(a#) = 7 THEN PRINT "???"
a# = LOG(128) / LOG(2) - .000000001#: PRINT INT(a#); a#
a# = INT(LOG(128) / LOG(2)): PRINT a#
PRINT LOG(4045)

Something in the code smells funny...

Data: LOG() returns a single..
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
oracle
*/-\*

Posts: 3652

 « Reply #19 on: July 14, 2003, 07:50:53 PM »

I made a rounding program to correct the rounding but. It's available in the FAQ.
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #20 on: July 14, 2003, 07:59:09 PM »

You're right, Aga, something smells funny.
*****
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #21 on: July 14, 2003, 08:03:49 PM »

Oracle,
Where is this FAQ re rounding? I also looked in QBNZ and couldn't find it there either.
*****
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #22 on: July 14, 2003, 08:38:59 PM »

There is a link in the top side of this forum, just below "Back to QbasicNews.com" and next to "QBOHO".

http://faq.qbasicnews.com
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
oracle
*/-\*

Posts: 3652

 « Reply #23 on: July 14, 2003, 08:50:58 PM »

The FAQ definately needs more promoting. I'm promoting it at QBNZ, but I think an admin should update the front page and add a link to it down the LHS.
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #24 on: July 14, 2003, 10:10:36 PM »

Found it. But what heading is it under?
*****
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #25 on: July 14, 2003, 10:16:41 PM »

http://faq.qbasicnews.com/?blast=RoundingNumbers

It's under misc.
 Logged

Glenn
I hold this place together

Posts: 786

 « Reply #26 on: July 14, 2003, 10:28:40 PM »

in your list of cases.  (Have a good time.)

Quote from: "Agamemnus"
because 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 and 16384 are all powers of two, and code (that I forgot) that gets the max bits used for a number (how many bits are needed to represent it) that is then subtracted from 2^those bits (or the array in the beginning) if equals zero, then it is a power of two.

Or, do this:

Code:

sub power.of.two% (n%):
select case n%
case 2, 4 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384
power.of.two% = -1
end select
end function
 Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Glenn
I hold this place together

Posts: 786

 « Reply #27 on: July 14, 2003, 10:33:28 PM »

I've been playing those fun and games with fortran since before some people here have been born.  (I also have fun, for example, taking the arccos of numbers that can't possibly be larger than one, but yet...are.)      It's caused by something that transcends specific programming languages or compilers.

Quote from: "Moneo"
You're right, Aga, Glenn's explanation regarding the fact that LOG gives a floating point result, basically answers my question.

However, if for n=128 the result of log(128)/log(2) is slightly less than 7, then why if you do:
a%=log(128)/log(2)
does it convert this result which is slightly less than 7, to a 7 which it puts into a%?

In other words, this result which is slightly less than 7 is treated as a 7 in one case, and not a 7 when I do all on one line.
THIS IS WHAT BUGS ME!

Somehow Basic is deciding how close a number has to be for it to ROUND it to an integer.
*****
 Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Moneo
Na_th_an

Posts: 1971

 « Reply #28 on: July 16, 2003, 05:58:11 PM »

Quote from: "oracle"

ORACLE,
In your ROUND function, I would include a test if the input number is negative, and if so round by -.5
Don't you agree?
*****
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #29 on: July 16, 2003, 08:39:08 PM »

Done

Did it work for your ^2 program?
 Logged

 Pages: 1 [2] 3