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 / Making a calculator

Author
Message
Novaguy
17
Years of Service
User Offline
Joined: 1st Apr 2009
Location:
Posted: 3rd Apr 2010 23:40 Edited at: 4th Apr 2010 03:12
For a quick background, my math professor put up an equation on the board and said "this is the accurate equation for calculating this probability, but computers can't hold enough digits to actually calculate it. Instead, we use this approximation..."
Anyhow, I thought it silly that a computer couldn't hold enough digits, so I'm trying to make a basic calculator that can +, -, *, /, ^, and !, but it does everything using strings. By using strings, I should be able to have up to DWORD digits (4 billionish).

I need a way to add x amount of 0's to a number without having to do this:



If x is very large, that for-next loop takes a while. It seems like there should be a better way to add 0s. I have one idea that goes something like:



This way adds more 0s faster, but there's the obvious over-count problem I'd have to account for. Any ideas?

edit: resolved
I just realized how silly it was to add 0's to the end of a decimal so I could do addition. Instead of lining up the numbers, I lined up the decimals like kindergartners do. I r moron.
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 5th Apr 2010 03:24
Quote: "I just realized how silly it was to add 0's to the end of a decimal so I could do addition. Instead of lining up the numbers, I lined up the decimals like kindergartners do. I r moron."


Sorry, I know this is resolved, but could you explain what you meant by this? I'm interested.

Windows 7 32-Bit Home Premium Intel Pentium Dual-Core @ 1.46Ghz 2038mb RAM
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 5th Apr 2010 07:34
@novaguy.

You should really read this:

http://en.wikipedia.org/wiki/Scientific_notation

http://en.wikipedia.org/wiki/Floating_point

BTW, strings are not and efficient way of storing 0 to 9...

Best regards

JF
Novaguy
17
Years of Service
User Offline
Joined: 1st Apr 2009
Location:
Posted: 5th Apr 2010 08:36 Edited at: 5th Apr 2010 08:39
I realize strings aren't an efficient way to do calculations, but DBPro has nothing larger than double integer. There are some C++ libraries like bigint and all of its siblings, but I wanted to try my hand at it just for kicks. I actually am having this adding zeroes problem still when multiplying (fixed it for addition though). Take a piece of paper and multiply 999*9999 and watch the steps you take. Put the larger one on top. You should get 89991+899910+8999100. When numbers get large, appending the ends with 0's can take down my speed a lot. The current way I do this was described above. Pretend you had a value stored as a string, like value$="48785729". If you want to multiply by 10, you simply do value$=value$+"0", and that appends a 0 on the end. If you wanted to multiply by 10^100, you have to append 100 times, or



Obviously, bigger numbers take longer to append. I'm probably going to end up making a whole different function for adding all of the elements you get from multiplication (the 89991+899910+8999100 from before) by shifting. It's probably the right way to do it, but I wanted to save time and use my addition function I made before.

As a side note, division is going to be a problem. If the computer keeps dividing a term until it reaches the end, it'll go into an infinite loop trying to calculate a never ending fraction, whether it be repeating or non-repeating. I guess the only way to do it best is smash every term into one big fraction, then do the final division last. I'm hoping for better ideas before I do that though.

@jfroco
I'm not a moron... Hope you know I was joking when I said that before. Scientific notation and float point variables won't calculate pi as accurately as I want. The end goal of this is to calculate pi to the nearest digit my screen can handle (about 10752 digits in the default font with 1440x900 resolution). Float points give pretty good approximations, but that's not what I'm after. As stated before, there are C++ libraries that do pretty much what I'm trying to do here but faster and more efficiently, but I'm doing this as a challenge to myself.

@Mad Nightmare
Hopefully the above described a little better of what I was trying to do.
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 5th Apr 2010 08:58
Hi novaguy,

Please don't misunderstand me.. I really believe you should check scientific notation article in Wikipedia and you will find what you want to do (multiply numbers, for instance) without having to add 0's.

"Scientific notation is a very convenient way to write large or small numbers and do calculations with them"

Scientific notation doesn't mean low accuracy... you can use all your 10752 digits as the significant digits in the scientific notation and you don't waste any of those for 0's.

Most operations are simpler when you use scientific notation, multiply by 1000 for instance is just as simple as exponent=exponent + 3.

Best regards
Novaguy
17
Years of Service
User Offline
Joined: 1st Apr 2009
Location:
Posted: 5th Apr 2010 10:06
Using scientific notation for adding (ex: 111*10^3 + 111*10^34) has no benefit unless I'm not understanding what you're getting at.

From the example above, try 89991+89991*10+89991*10^2 without expanding the exponents (aka use scientific notation). It makes things a lot harder. The majority of numbers actually don't have long chains of 0's stuck on the end of them.

My multiplication is done through adding the elements as described above. I really can't think of a faster way to do it unless someone enlightens me. Sure, if my numbers contained large amounts of 0's on the ends of them, it'd cut back on a few for-next cycles, but I still have the problem of shifting elements to add them together after the scientific notation shortcut is taken and the significant digits start.

If it's still unclear what I'm looking for, try writing a function multiply(u$,v$) that will output u*v in a string. You're not allowed to str$(val(u)*val(v)). You'll see what I mean.
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 5th Apr 2010 10:40
Novaguy,

I don't think you read the article so I see I shouldn't have answered in the first place

The example you gave (addition of numbers with different orders of magnitude) is of course the worst case scenario for scientific notation... but it doesn't matter as in programming is not common to add numbers with three different orders of magnitud.

Anyway, if you want to do it, from wikipedia:

"Addition and subtraction require the numbers to be represented using the same exponential part, so that the significant can be simply added or subtracted"

So you need to add the 0's, so the worst case scenario is what you are doing now.

But, What about multiplication and division?

89991*10^0*89991*10^1+89991*10^3 is (89991*89991*89991)*10^(0+1+3)

Anyway, good luck with your projet...disabling mailback
Novaguy
17
Years of Service
User Offline
Joined: 1st Apr 2009
Location:
Posted: 6th Apr 2010 02:37
Since mailback is disabled you probably won't get this, but I do feel the need to defend myself. First of all, I know how scientific notation works.

"89991*10^0*89991*10^1+89991*10^3 is (89991*89991*89991)*10^(0+1+2)"
Looks real simple. So what's 89991*89991*89991? Now we're back to using an algorithm that requires... shifting with 0's! As an example:
89991*89991=1*89991+90*89991+900*89991+9000*89991+800000*89991. This is the method kindergartners use to multiply out large numbers. It requires shifting 0's, which brings me back to my original problem. As I said before, multiplying and dividing with scientific notation may save a few cycles, but then adding and subtracting with scientific notation adds many many many more cycles back on. Scientific notation is great for rounding, but horrible for precision answers. In the end, it'd be much slower.

If addition of numbers is a worse case scenario for scientific notation (and I agree that it is) then I definitely shouldn't use it since there will be TONS of addition (there's addition required in the multiplication algorithm, and multiplication in the exponent algorithm. Lots of addition).

Login to post a reply

Server time is: 2026-07-26 19:47:53
Your offset time is: 2026-07-26 19:47:53