Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP: Why does this not work?
#31
Quote: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.
*****
Reply
#32
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
[url]
http://forum.qbasicnews.com/viewtopic.php?t=3455
[/url]
Jark is using in his help the same "feature" that gives you problems
Antoni
Reply
#33
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...
hink Global, Make Symp' All ! ®
[Image: Banner.gif]
Reply
#34
i have to say that

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

works for me.





[Flexibal>
Reply
#35
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.
*****
Reply
#36
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.
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)
Reply
#37
Thanks, SCM. Yeah, I'll have to use 2 lines of code.
*****
Reply
#38
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"
Reply
#39
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.
*****
Reply
#40
I've to apologize, this piece of code is not my, it's from "c snippets".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)