notes-computer-programming-programmingLanguagesBook-programmingLanguagesChNumbers

Table of Contents for Programming Languages: a survey

Chapter : numbers and arithmetic

the mathematical number type hierarchy

the C number type hierarchy

arithmetic

integer arithmetic

floating point arithmetic:

IEEE754 1985, 2008

https://en.wikipedia.org/wiki/IEEE_754-2008

https://en.wikipedia.org/wiki/ISO/IEC_10967

https://en.wikipedia.org/wiki/C99#IEEE.C2.A0754_floating_point_support (see also C 2011, ISO/IEC 9899:201x)

fixed point arithmetic:

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1169.pdf

decimal arithmetic

rational arithmetic

interval arithmetic: https://www.semipublic.comp-arch.net/wiki/Interval_arithmetic

common operations

literals

addition, subtraction

multiplication, division

bitwise OR, AND, XOR

exponentiation, log, sqrt often library fns

subtraction is binary but negation is unary

often the symbol - is used in two ways: a - b for subtraction, but -b for the negation of b (e.g. if b is 3, then -b is -3)

this irregularity can be vexing. e.g. in Haskell, the expression "abs -3" is parsed as "abs (-) 3" and you have to write "abs (-3)". some languages use another character for one of subtraction or negation, e.g. ML uses ~ (tilde) for unary negation.

common syntax

infix

promotion

traditional precedence

undefined results in integer arithmetic

divide by zero

Zero to the zeroth power

There is some disagreement in mathematics whether the result of 0^0 should be one, or undefined. IEEE754 2008 defines various different functions for exponentiation: pow, pown (integers), powr (reals). pow gives 0^0 = 1. pown gives 0^0 = 1. powr gives 0^0 = nan.

In many langauges and platforms (e.g. C, Python, Java, Octave, C++, .NET), 0^0 returns 1. (cite https://en.wikipedia.org/wiki/Exponentiation#Programming_languages )

integer overflow

if your integers are stored as fixed-size fields in memory, you have the possibility of getting a result that is too large to fit in that memory field.

at least six ways to deal with this:

vectorization

matrics as linear operations vs. 'matrices' as just multidimensional arrays