>
> can anybody explain the meaning of the length function in Integer? I thought
> that should be the length of the binary expansion,
> at least for i >= 0:
>
For i >= 0 it is length of binary expansion. More precisely, it is
smallest number of binary digits (excluding sign bit ) needed to
express i exacty in two-complement notation. In two-complement
there are two numbers which can be expressed sign + 0 bits: 0 (with positive
sign) and -1 (with negative sign). It is easy to see that using
i + 1 bits (the '+ 1' part is becaus we ignore the sign bit) in two
complement notation it is possible to represent numbers from -2^i to
2^i - 1. So
length(i) = min { j >= 0 : -2^j \leq i \leq 2^j - 1}
This definition may look fishy, but it is exactly what is needed
when computing how many bits you need to store numbers in given
range.
> (248) -> for i in -8..8 repeat print [i::ANY,i::BINARY::ANY,length i]
> [- 8,- 1000,3]
-8 = ...1000, the leading 1 is sign bit, so 3 other bits
> [- 7,- 111,3]
-7 = ...1001, again 3 non-sign bits
> [- 6,- 110,3]
-6 = ...1010
> [- 5,- 101,3]
-5 = ...1010
> [- 4,- 100,2]
-4 = ...1100, so indeed only 2 non-sign bits
> [- 3,- 11,2]
> [- 2,- 10,1]
> [- 1,- 1,0]
-1 = ...1, so there is only sign bit, _no_ nosign bits so inded 0
> [0,0,0]
> [1,1,1]
> [2,10,2]
> [3,11,2]
> [4,100,3]
> [5,101,3]
> [6,110,3]
> [7,111,3]
> [8,1000,4]
> Type: Vo=
> id
>
>
>
> Mit freundlichen Gr=FC=DFen
>
> Johannes Grabmeier
>
> Prof. Dr. Johannes Grabmeier
> K=F6ckstra=DFe 1, D-94469 Deggendorf
> Tel. +49-(0)-991-2979584, Tel. +49-(0)-171-5503789
> Tel. +49-(0)-991-3615-100 (d), Fax: +49-(0)-1803-5518-17745
>
> --=20
> You received this message because you are subscribed to the Google Groups "=
> FriCAS - computer algebra system" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to fricas-devel+unsubscribe@goog=
> legroups.com.
> For more options, visit this group at http://groups.google.com/group/fricas=
> -devel?hl=3Den.
>
>
--
Waldek Hebisch
[email protected]
--
You received this message because you are subscribed to the Google Groups
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fricas-devel?hl=en.