<Omikron.Basic> IF .. >0 and <1

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
Omikronman
Atari Super Hero
Atari Super Hero
Posts: 525
Joined: Wed Dec 01, 2004 12:13 am
Location: Germany
Contact:

<Omikron.Basic> IF .. >0 and <1

Postby Omikronman » Tue Aug 16, 2011 7:16 am

Hello,

I am looking for a faster way to test if a value is >0 and <1. I think it would be useful to avoid using the AND command. I can also use THEN IF, but I like to avoid two IF to test it. :)

Tnx, Omi

Dal
Administrator
Administrator
Posts: 4079
Joined: Tue Jan 18, 2011 12:31 am
Location: Cheltenham, UK
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby Dal » Tue Aug 16, 2011 8:49 am

What is it you want to do with these values? Filter them out or are you only interested in values between 0 and 1?

My first thought is to peek the address of the variable and test the first two bytes are zero.

This approach would also include zero itself of course. Maybe someone else has a better approach?
TT030: 4MB/16MB + Crazy Dots, Mega"SST" 12, MegaSTE, STE: Desktopper case, IDE interface, UltraSatan (8GB + 512Mb) + HXC floppy emulator. Plus some STE's/STFM's

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: <Omikron.Basic> IF .. >0 and <1

Postby Nyh » Tue Aug 16, 2011 2:39 pm

Omikronman wrote:Hello,

I am looking for a faster way to test if a value is >0 and <1. I think it would be useful to avoid using the AND command. I can also use THEN IF, but I like to avoid two IF to test it. :)

>0 AND <1 makes only sense with floating point. Drop the floating point and get a lot faster.

In most cases floating point can be replaced by 32 or even 16 bit fixed point calculations.

Hans Wessels

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2057
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby charles » Sat Aug 20, 2011 11:38 am

sometimes i do a double single line to replace the and
but its often defeated by the compillied program ..just so its not doing a comparision
twice

if x<1 then

if x>0 then
code
code
code
endif

endif

now it instantily rules out numbers 1 and higher without checking the and comand
evertime

charles
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
Omikronman
Atari Super Hero
Atari Super Hero
Posts: 525
Joined: Wed Dec 01, 2004 12:13 am
Location: Germany
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby Omikronman » Sun Dec 11, 2011 11:16 pm

Ah, I have already asked for that. Today I examine my simulation again and I still wonder if there is an easier way to ask "if x! >0 then if x!<1 then..." I am not sure if it will be faster to use two IF lines instead of using the "THEN IF" in the same line. My hope was to be able to test it with one IF perhaps. O.o

User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby daeghnao » Wed Dec 14, 2011 8:39 am

Regarding the different ways of constructing the test, almost all of the time you'll find that when given A AND B the programming language evaluates A and if it's false then it won't even worry about B. It's called short-circuit evaluation. If you think about the way the tests are likely to be implemented in compiled code, with comparisons and jumps, it kind of makes sense. There are other reasons to split out a test into multiple parts, but they're not to do with the efficiency of the implementation. If in doubt, look at the object code and find out what it's actually doing.

Regarding a test for a floating-point number being between 0 and 1, it really depends on the in-memory layout of the floating-point format, but you should be able to test the bit-pattern to make sure it's positive and either a denormalised number (if your calculations go down that far) or a normalised number with a low enough exponent. It'll be a bit-masking operation and then a comparison or two, I suspect, but I've only just thought of it. You'll need to study the manual of your floating-point system carefully and do a bit of testing to find out if it's feasible and if it's any faster than just writing out the comparisons. As said before though, depending on the application, you will likely be much better off using fixed-point algorithms.

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2057
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby charles » Thu Dec 15, 2011 6:11 am

well i think that the program he has weitten is making numbers other than in the range 0-1 so it is loosing time just making numbers


and further

what is wrong with this
if a!<1 then
if a!>0 then
endif
endif

thanks
charles
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!

User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby daeghnao » Thu Dec 15, 2011 10:06 am

charles wrote:well i think that the program he has weitten is making numbers other than in the range 0-1 so it is loosing time just making numbers


That's actually a good point behind what you've written there. What algorithm is this that requires a test for a number between 0 and 1? Is there a better algorithm that is more efficient? Maybe OP can divulge further?

charles wrote:and further

what is wrong with this
if a!<1 then
if a!>0 then
endif
endif

thanks
charles


1: if this is an interpreted language, then the language parser has to do two statement parses and keep track of two nested blocks. Using a single expression, it only has to parse one statement and keep track of one block. It depends on whether it does this each time it gets to these statements and whether expressions are easier to process than statements, of course.

2: if compiled, there is probably nothing wrong with the structure, it'll come out the same as "if a! < 1 and a! > 0". But if you need to review the code, add to the expression, or do something complicated inside that branch, then things are going to get harder and harder to keep track of. It's better if the thing you write in the program has as much relation to what you mean as possible, then you let the language intepreter/compiler do all the messy stuff for you.

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: <Omikron.Basic> IF .. >0 and <1

Postby Nyh » Thu Dec 15, 2011 10:40 am

daeghnao wrote:You'll need to study the manual of your floating-point system carefully and do a bit of testing to find out if it's feasible and if it's any faster than just writing out the comparisons. As said before though, depending on the application, you will likely be much better off using fixed-point algorithms.

If you want to go this far for optimizing I would first take a good look at the algorithms. Making you algorithms smarter can improve speed by orders of magnitude. Trying to optimize code might give you 10% improvement, when you are lucky.

I think best way to improve speed here is to ditch the floating point numbers and go to fixed point 32 bit numbers. (I already did say so, didn't I?)

Hans Wessels

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2057
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: <Omikron.Basic> IF .. >0 and <1

Postby charles » Fri Dec 16, 2011 4:19 am

i hear you loud and clear nyh..
good scheme

yeah use 32 bit ...or something crazy like

find a method to generate 32 bit number
and do calculatons with it ,

when needed you can convert to floating point ,
,,this appears very quick , and eliminates the checking
example.......

a%=random(21473847)==={i foget 32 max!! sorry)

then

use it aS THE DECIMAL PLACE DIDGITS

SO IF A% IN THE EXAMPLE WAS 67890
COMPUTER CAN QUICKILY USE A 32BIT #
THEN GRAPHIC OUTPUT NEWILY CONVERT TO .67890

UNDERSTANDABLE?
THIS ELIMINATES ANY >0 AND <1 CAUSE ANY # WITH A DECIMAL IN FRONT OF IT IS ALWAYS UNDER /LESS THEN 1


CHARLEDS
atari is my lifestyle,not a hobby.
HOLD ON ! ! !,
Im printing unreadable characters ...!


Social Media

     

Return to “Other BASIC”

Who is online

Users browsing this forum: No registered users and 1 guest