Qbasicnews.com
March 28, 2020, 06:36:25 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 2 [3]
  Print  
Author Topic: HELP: Why does this not work?  (Read 9081 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #30 on: July 16, 2003, 09:19:59 PM »

Quote from: "oracle"
Did it work for your ^2 program?


The results of my power of 2 program is that it works fine if you do it with 2 lines of code. I have a version of the one line of code which takes an INT and rounds by .5, but I can't justifiy doing all this just to make it work. In short, I don't trust it. If I had to do this in a production program I would definitely resort to doing it in 2 lines of code.

Thanks for your interest and support.
*****
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #31 on: July 25, 2003, 06:01:39 AM »

Moneo:
Probably the answer to your initial question is qb is using an increased precision in intermediate results. If you store the value in a variable it is rounded to the standard precission.

This thread gave me the idea
http://http://forum.qbasicnews.com/viewtopic.php?t=3455
Jark is using in his help the same "feature" that gives you problems
Logged

Antoni
Jark
Ancient Guru
****
Posts: 566



WWW
« Reply #32 on: July 25, 2003, 12:21:43 PM »

Slightly out of scope, but related anyway:

I unfortunately don't have a example of cases where the issue occurs, but I decided months ago to use INT(x!+.5) instead of CINT. I noticed some problems with CINT, and I got rid of them with this old trick...
Logged

hink Global, Make Symp' All !
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #33 on: July 25, 2003, 12:56:43 PM »

i have to say that

if 2^(log(n)/log(2)) = n then print "cuss word"

works for me.





[Flexibal>
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #34 on: July 25, 2003, 03:54:47 PM »

ANTONI,
Thanks. I looked at the thread you mentioned, and like you said: "It's curious!" That is, QB uses one value in an internal calculation, but changes it if you store it into a variable. That is the root of the problem.

JARK,
At least 10 years ago I too discovered some weirdness with CINT. I don't remember exactly what it was, but since then I never use CINT at all. Must be the problem you mention.

FLEXIBAL,
No, the following "if" does not result true if n=128.
if 2^(log(n)/log(2)) = n then .....

That was the whole issue of this thread.
*****
Logged
SCM
Wandering Guru
***
Posts: 311



« Reply #35 on: August 01, 2003, 04:05:28 AM »

Maneo,
I tried your procedure in MatLab and it worked fine.  There is an error in BASIC in the 20th digit of LOG(128)/LOG(2).  This also results in an error in the 20th digit of 2^(LOG(128)/LOG(2)).  Why BASIC doesn't consider 127.999999999999999951 the same as 128 I don't know (even in double precision any difference beyond the 16th digit should be neglected), but your two line code compensates for this by rounding 6.99999999999999999957 to 7 when it assigns it to a.  I think you'll have to be content with a two line procedure that works perfectly.
Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #36 on: August 01, 2003, 03:51:18 PM »

Thanks, SCM. Yeah, I'll have to use 2 lines of code.
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #37 on: August 08, 2003, 04:29:46 AM »

This message is too late but ...

Quote

dim a as long
input a
if (a>0) and ((a and (a-1))=0) then print a; " is a power of 2"
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #38 on: August 08, 2003, 03:29:46 PM »

XHANTT:

It's never too late for an absolutely brillant and simple solution!

We all now have an excellent one-line algorithm for determining if a number is a power of 2. Thanks.

The logic is the same as when we want to cycle throught a range of numbers from 0 to a power of 2, minus 1. After incrementing, instead of doing an "if" to see if at the limit, you just "and" with the limit (power of 2, minus 1) and the cycle automatically begins with zero again. Many of us have seen this logic, but it never occurred to us to use it for determining powers of 2.
*****
Logged
xhantt
Member
*
Posts: 90



« Reply #39 on: August 08, 2003, 09:01:27 PM »

I've to apologize, this piece of code is not my, it's from "c snippets".
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #40 on: August 09, 2003, 01:45:15 AM »

XHANTT,
No need to apologize. You recognized an excellent solution and posted it for us. It's not important where it comes from.
*****
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #41 on: August 11, 2003, 12:27:29 AM »

Can that be generalised to a^n? Cos that method is really interesting.
Logged

na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #42 on: August 11, 2003, 05:45:42 AM »

Nope. That method woks 'cause of the binary nature of the AND operator.
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



WWW
« Reply #43 on: August 11, 2003, 07:47:42 PM »

Awww... (ambitious StatLib plans down drain) :lol:
Logged

Pages: 1 2 [3]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!