Qbasicnews.com

Full Version: Challenge: Algorithms having only one line of code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10
O.K., Moneo, you have had a few responses. Now, what are you going to do with them? I envision you will cull them, arrange them in some order, with a nice description preceding each one, and sort of customize there presentation, besides providing a neat identifiction tag for each one. If that is your intention, it might be a good idea to let the folks know your intentions, publish what you have so far, and further invite more posts on the subject.

Regards,

Ralph
Quote:O.K., Moneo, you have had a few responses. Now, what are you going to do with them? I envision you will cull them, arrange them in some order, with a nice description preceding each one, and sort of customize there presentation, besides providing a neat identifiction tag for each one. If that is your intention, it might be a good idea to let the folks know your intentions, publish what you have so far, and further invite more posts on the subject.

Regards,
Ralph
I revived this old thread for the purpose of correcting a rounding algorithm that I had posted back in 2003. I was concerned that someone might be using that old algorithm and sometimes getting wrong results while rounding negative numbers.

Then people started posting other algorithms, and the thread got started up again.

I already have a tutorial on algorithms which I wrote a few years ago for QBNZ. I might consider updating this tutorial for publication on the QB Express, in which case I'll scan this entire thread again to pick up any algorithms which in my opinion are worth including. This tutorial already gives credit to the author of each algorithm.

I agree with you had I intended to publish these algorithms. Yes, letting everyone know would have been a good idea. However, as I said before, this was not my intention. If you have the inclination to publish these algorithms, you're entirely free to do so.
*****
Heres the plan

1. Ask for Algorithms.
2. ??????
3. Profit
Heres a couple i came across recently that i found quite interesting. They are for testing if an addition or subtraction causes an overflow (ie the result is out of signed range)

These examples assume that a and b are 16-bit signed values. This can be changed by changing the AND &H8000 to other values (ie &H80 for 8-bit).

Here is a link to a topic where redcrab explained to me exactly how they work

http://www.freebasic.net/forum/viewtopic.php?t=3914

Addition
Code:
overflow = ((a XOR (NOT b)) AND (a XOR (a + b)) AND &H8000) <> 0

Subtraction
Code:
overflow = ((a XOR b) AND (a XOR (a - b)) AND &H8000) <> 0
Vert good, Yetifoot, these algorithms for detecting overflow could come in handy for not having to use ON ERROR.
*****
I think they would have to be modified for use in QB, because the code itself will cause an overflow (due to a+b, or a-b). They are more useful for FB, because FB does not do overflow checks (unless it's switched on with -ex, or -exx).
QBasic does not signal overflow if compiled without the /d option. Only interpreted QB always signals overflows, and your code would fail there.
Quote:QBasic does not signal overflow if compiled without the /d option. Only interpreted QB always signals overflows, and your code would fail there.

Thanks for the clarification, I have not used QB/QBASIC much so I don't know much about it.
Pages: 1 2 3 4 5 6 7 8 9 10