]> www.ginac.de Git - cln.git/blob - src/float/misc/cl_float_format.cc
Initial revision
[cln.git] / src / float / misc / cl_float_format.cc
1 // cl_float_format().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_float.h"
8
9
10 // Implementation.
11
12 cl_float_format_t cl_float_format (uintL n)
13 {
14 // Methode:
15 // Mindestens 1+n Dezimalstellen (inklusive Vorkommastelle)
16 // bedeutet mindestens ceiling((1+n)*ln(10)/ln(2)) Binärstellen.
17 // ln(10)/ln(2) = 3.321928095 = (binär) 11.01010010011010011110000100101111...
18 //                       = (binär) 100 - 0.10101101100101100001111011010001
19 // Durch diese Berechnungsmethode wird das Ergebnis sicher >= (1+n)*ln(10)/ln(2)
20 // sein, evtl. um ein paar Bit zu groß, aber nicht zu klein.
21         n = 1+n;
22         return (cl_float_format_t)
23                ((n << 2)
24                 - (n >> 1) - (n >> 3) - (n >> 5) - (n >> 6) - (n >> 8)
25                 - (n >> 9) - (n >> 12) - (n >> 14) - (n >> 15)
26                );
27 }