Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / What wrong with my algorithm :( ?

Author
Message
SH4773R
16
Years of Service
User Offline
Joined: 18th Jan 2010
Location: AMERICA!!!
Posted: 1st Sep 2010 05:53 Edited at: 1st Sep 2010 06:01
well im writing a benchmark program that sums fractions but in the total sum i some times get nagitive numbers?

here is my summing code and my varible explantions









heres the whole thing:




Who the hell is General Failure? And why is he trying to read my hard disk?

My software never has bugs. It just develops random features.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Sep 2010 06:19 Edited at: 1st Sep 2010 07:17
As far as I can see this is what you are doing:

Example using 1/2 and 3/4...
Common Denominator = 4*2 = 8
Scale Numerators To CD = 1*4 + 3*2 = 10
but then you just add 10 to r4, 10 isn't the answer, it is 10/8: (r1+r2)/d12.

You are probably getting negative numbers because the integer is overloading, I'll explain: in your program you can have a fraction of 999/1000 so by your current method, assuming both fractions are 999/1000, you'd get this answer:

r1 = 999*1000 = 999,000
r2 = 999*1000 = 999,000
r3 = r1+r2 = 1,998,000

now if you run the program for 60 seconds it is going to be able to execute your loop thousands of times, lets say 2000 times (that would be once every 30ms which is probably slower that it would run), that means your sum at the end of the program would be

1,998,000 * 2000 = 3,996,000,000

I forget what the limit is for signed integers in DB but I'm pretty sure this is a big enough number to tip the scales. The reason you get a negative number is because of the way numbers are stored in memory. Let's say we have a puny computer that can only store 4-bit numbers, now if you know anything about binary you will know the biggest number we can make with 4 bits is 15:
1 2 4 8
x + x + x + x
1 1 1 1

Buuut, if we want to be able to do negative numbers we're going to have to use one of those bits to remember if the number is positive or negative. So our bits would be worth: -,1,2 and 4. You see how we can now only make -7 to +7, (note:this is why it's important to use the right data type, if we're not going to use negative numbers we should use an unsigned data type which allows bigger numbers). So what happens if we overload the data type? Let's count up until we overload...

0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = -7
1001 = -6
1010 = -5
1011 = -4
1100 = -3
1101 = -2
1110 = -1
1111 = -0

Did you wonder why 1000 is -7 and not -0? If we add one more you'll see why...

1111 = -0
0000 = 0

Using the loop of the bits in this way helps us use numbers as they are used in the real world, negative numbers decrease in value as they are added to until they eventually become positive.

I hope that helps.

PS. If you want to find out for yourself what the highest number you can store in an integer is you can write a very simple program that will show you. Try to figure it out before looking in this box.


SH4773R
16
Years of Service
User Offline
Joined: 18th Jan 2010
Location: AMERICA!!!
Posted: 1st Sep 2010 07:39
are there anythings like longs and floats i can use?
btw thanks for the very long and educational post

Who the hell is General Failure? And why is he trying to read my hard disk?

My software never has bugs. It just develops random features.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Sep 2010 09:23 Edited at: 1st Sep 2010 09:26
you can use a dword (unsigned integer ~= double the maximum number)

Support for double integers is weird (unsigned double ints go up to 18446744073709551615). You might want to look it up - double floats aren't supported at all (to my knowledge, they act *exactly* like floats).

If you don't need 100% accuracy (Which you really never do), you can use floats. Their format is basically scientific notation, with approximately 7 digits accuracy (except for *really really* small numbers - less than 2^-127)

Login to post a reply

Server time is: 2026-07-24 23:12:38
Your offset time is: 2026-07-24 23:12:38