Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2007-05-30 23:56:55

Kurre
Member
Registered: 2006-07-18
Posts: 280

Programming problem

When I am programming my computer cant handle big numbers. If i do an operation where the result exceeds maybe ~10 digits, i get a negative value or just a wrong value. Why cant the computer handle greater numbers? How can i make them do that? I am using Delphi 7. Any help would be appreciated!

Offline

#2 2007-05-31 00:55:18

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

Re: Programming problem

computers store values as bits, most computers can only store 32bits (64bits for 64bit processors)

for an unsigned integer (positive only) this means you can only store the integers 0 to 4294967296 (0 to 2^32)
and for signed integers (positive and negative) you can store the integers -2147483647 to 2147483648

what happens when you get to 0xffffffff, is if you try to add 1 to it for example, it will have an overflow -> 0xffffffff + 1 = 0x100000000, which is 33bits, but computer can only store 32bits, so the extra overflow bit is ignored, and you get back to 0

for unsigned integers, it means that once you reach 4294967296, it will loop back round to 0, and for signed integers,  once you reach 2147483648 it will loop backround to -2147483647

----

floating point numbers are generally the solution to this sort of problem, floating point numbers  of type 'float' will use some bits to store the number, another bit for the sign, and 3 bits i think for the power of 10 with additional bit for sign of power, i.e. it stores numbers in scientific notation, what this means is that you have a limit of accuracy, numbers can only have so many digits, but you can store very small or very large numbers.

the type 'double' generally uses 64bits, (2 memory address of 32bit) and stores numbers with up to 15 digits, with 4 bits for the power so you can have numbers like 1.55579105400234e-250

(dont quote me on how many bits im saying its using, but you get the general idea)

ive never used delphi, but it will most deffinately have support for floating point numbers

Last edited by luca-deltodesco (2007-05-31 01:00:25)


The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

#3 2007-05-31 01:45:10

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Programming problem

Floating point numbers are unreliable however.  Because they use powers, they are only an approximation.

Instead, use bit strings to do whatever you want.  Say you have a number that takes 384 bits to store.  Then simply allocate a string of that many bits, and store it.  But be warned, numeric computations will often be very slow.  That, and you have to program them all (such as addition) yourself.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

Board footer

Powered by FluxBB