NetLogo banner

 Contact Us

 Modeling Commons

 User Manuals:


NetLogo User Community Models

(back to the NetLogo User Community Models)


by Michael Kuyumcu (Submitted: 10/02/2004)

[no screen shot for this model]

Download Bignum_Routines
If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

(You can also run this model in your browser, but we don't recommend it; details here.)


This is a collection of reporters for doing calculations with arbitrary-precision integer and floating-point real and complex numbers. In the collection you find the following reporters which accept both STRINGS and normal NetLogo numbers as arguments. In the case of complex numbers, for each complex number, one 2-element list of strings (real part and imaginary part) are passed. Results are ALWAYS passed back to the caller as STRINGS (real floating-point numbers) or 2-element lists of strings (complex numbers).

Example: set c real_mul "1288272.827727272727" "188272727.292929299229922992292971"
(Result for c: "242546638773587.705228117835542710057003926002101917")

or, one example for complex numbers:
set c complex_mul ["127.18872" "-19.277271882"] ["-0.2377161" "8.7272662"]
(Result for c: "[138.0030768415969884 1114.5923349676927002]")

int_add a b : adds two large - signed or unsigned - integer numbers
int_sub a b : subtracts b from a (signed or unsigned)
int_mul a b : multiplies large - signed or unsigned - numbers

real_add a b : adds two large - unsigned or signed - floating-point numbers
real_sub a b : subtracts a large floating-point number a from b
real_mul a b : multiplies two large floating-point numbers
real_div a b precision : divides two large floating-point numbers with
#precision digits
real_nth_power a nth : raises a (a real value) to the non-negative integer
power n
real_nth_root a nth accuracy : calculates the nth (n an integer) root of a
: which is correct to #accuracy decimal digits
real_sqrt a accuracy : calculates the square root of a
: which is correct to #accuracy decimal digits
real_cube_root a accuracy : calculates the cube (3rd) root of real number a

complex_add a b: sum of two complex numbers (handed as 2-element lists
of strings), for instance:
set c complex_add ["12.11" "-0.1"] ["-9.881" "2.112"]
The first list element is the real part, the 2nd the imaginary
complex_sub a b: calculates the complex difference a - b.
complex_mul a b: calculates the comples product a * b
complex_div a b precision: calculates a / b (b must not equal ["0" "0"])
to the desired accuracy.
complex_conjugate a: computes the complex conjugate of a complex number a
complex_modulus_squared a: computes the squared modulus of a complex number a
complex_modulus a precision: gives the square root of complex_modulus_squared
: with the desired precision
real_part a : gives the real part of the complex number a
imag_part a : gives the imaginary part of the complex number a

real_sgn a : reports the sign of a number (1 positive, 0 zero, -1 negative)
change_sign a : multiplies a and (-1)
real_abs a : returns -a for negative a, and a for positive a
int_part a : reports the integer portion of a floating point number a
fract_part a : reports the fractional portion of a
rounded a dec_places : reports a rounded to #dec_places decimal places

integer? a : reports TRUE if a has no decimal point, FALSE otherwise
positive? a : reports TRUE if a > 0, FALSE otherwise
negative? a ; reports TRUE if a < 0, FALSE otherwise
zero? a : reports TRUE if a equals 0, FALSE otherwise

equal? a b : reports TRUE if a equals b, FALSE otherwise
greater? a b : reports TRUE if the value of a is greater than b's
less? a b : reports TRUE if a < b, FALSE otherwise
greater_or_equal? a b : analogous
less_or_equal? a b : analogous

string text times : reports a string with #text appended #times times.
: e.g. string "H" 10 -> "HHHHHHHHHH"

power10 number times : reports the product of number and 10 raised to the
: the power of #times

decimal_sameness a b dec_places : for #dec_places > 0, reports the number of
decimal places shared by a and b (continuous)
: for #dec_places = 0, reports whether a and b
have the same integer value
: This is reporter is useful for iterations


Pass arguments to the reporters mentioned above as normal NetLogo numbers or as STRINGS (for arbitrary-length and -precision numbers) containing the numbers to be processed in base 10 notation.

The most elaborate routines are int_mul for the multiplication of large integers (which is used for floating-point numbers as well). That reporter uses the TOOM-COOK algorithm which desiplays a much better runtime behavior than multiplying with the longhand "school" method, i.e. O(n^1.485) vs. O(n^2). At the heart of the floating point division algorithm is a standard Newton-Rhapson iteration. As a sufficiently close first approximation of the solution for 1/b in (a/b = a * 1/b) is crucial for this algorithm, the routines tries to find out a close guess, but it appears to be clumsy. Rewrite it with a better approach - and mail it to me! :)

My motivation in writing these routines was my interest in fractal geometry. For producing my own fractal exploration software, I need customized routines that can handle numbers with arbitrary precision (length). Most of the programs that have been written so far for creating Mandelbrot sets, Julia sets and images of Newton-iterated roots of complex functions are not capable of handling really high-precision numbers. As a result, the user can zoom into the calculated fractal image only a few times before the built-in arithmetic becomes too "coarse" for deep detail. But realizing how slow NetLogo is when it comes to string manipulation, I have decided to only use it as a rapid prototyping environment and will rewrite the routines in a different programming language after testing of the routines.


Run the test suite by pressing the button on the interface tab. You may use the routines provided here as you like.


Normally, when doing iterations and checking whether the old approximated value is sufficiently close to the new one, one subtracts both, calculated |Xold-Xnew| and checks if this is less a certain threshold epsilon. With the reporter decimal_sameness, the check is made "graphically" by comparing the significant decimal places instead of a full-blown subtraction and comparison (which in itself is another comparison). If you are interested in this approach, have a look at the WHILE loop in the real_div reporter.


call real_mul with really large numbers. Time the result. See how slow NetLogo string handling is.


Write reporters that draw roots (square, cubic, arbitrary).
Implement reporters for arbitrary powers (fractional), logarithms, trigonometric functions and a few for number theory like Li(n). Implement representations for continued fractions. Add routines for complex number calculations.




(back to the NetLogo User Community Models)