summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/inc/real2str.inc
blob: 73fb92b77ced7d095e1851f6f62a0265419f50cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by Michael Van Canneyt,
    member of the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

type
  { See symconst.pas tfloattype }
  treal_type = (
    rt_s32real,rt_s64real,rt_s80real,rt_sc80real,
    rt_c64bit,rt_currency,rt_s128real
  );
  { corresponding to single   double   extended   fixed      comp for i386 }

Procedure str_real (len,f : longint; d : ValReal; real_type :treal_type; out s : string);
{$ifdef SUPPORT_EXTENDED}
type
  TSplitExtended = packed record
    case byte of
      0: (bytes: Array[0..9] of byte);
      1: (words: Array[0..4] of word);
      2: (cards: Array[0..1] of cardinal; w: word);
  end;
const
  maxDigits = 17;
{$else}
{$ifdef SUPPORT_DOUBLE}
type
  TSplitDouble = packed record
    case byte of
      0: (bytes: Array[0..7] of byte);
      1: (words: Array[0..3] of word);
      2: (cards: Array[0..1] of cardinal);
  end;
const
  maxDigits = 15;
{$else}
{$ifdef SUPPORT_SINGLE}
type
  TSplitSingle = packed record
    case byte of
      0: (bytes: Array[0..3] of byte);
      1: (words: Array[0..1] of word);
      2: (cards: Array[0..0] of cardinal);
  end;
const
  maxDigits = 9;
{$endif SUPPORT_SINGLE}
{$endif SUPPORT_DOUBLE}
{$endif SUPPORT_EXTENDED}

type
  { the value in the last position is used for rounding }
  TIntPartStack = array[1..maxDigits+1] of valReal;

var
  roundCorr, corrVal, factor : valReal;
  spos, endpos, fracCount: longint;
  correct, currprec: longint;
  temp : string;
  power : string[10];
  sign : boolean;
  dot : byte;
  fraczero, expMaximal: boolean;


  maxlen : longint;   { Maximal length of string for float }
  minlen : longint;   { Minimal length of string for float }
  explen : longint;   { Length of exponent, including E and sign.
                           Must be strictly larger than 2 }
const
  maxexp = 1e+35;   { Maximum value for decimal expressions }
  minexp = 1e-35;   { Minimum value for decimal expressions }
  zero   = '0000000000000000000000000000000000000000';

  procedure RoundStr(var s: string; lastPos: byte);
  var carry: longint;
  begin
    carry := 1;
    repeat
      s[lastPos] := chr(ord(s[lastPos])+carry);
      carry := 0;
      if s[lastPos] > '9' then
        begin
          s[lastPos] := '0';
          carry := 1;
        end;
      dec(lastPos);
    until carry = 0;
  end;

  procedure getIntPart(d: valreal);
  var
    intPartStack: TIntPartStack;
    intPart, stackPtr, endStackPtr, digits: longint;
    overflow: boolean;
  begin
{$ifdef DEBUG_NASM}
    writeln(stderr,'getintpart(d) entry');
{$endif DEBUG_NASM}
    { position in the stack (gets increased before first write) }
    stackPtr := 0;
    { number of digits processed }
    digits := 0;
    { did we wrap around in the stack? Necessary to know whether we should round }
    overflow :=false;
    { generate a list consisting of d, d/10, d/100, ... until d < 1.0 }
    while d > 1.0-roundCorr do
      begin
        inc(stackPtr);
        inc(digits);
        if stackPtr > maxDigits+1 then
          begin
            stackPtr := 1;
            overflow := true;
          end;
        intPartStack[stackPtr] := d;
        d := d / 10.0;
      end;
    { if no integer part, exit }
    if digits = 0 then
      exit;
    endStackPtr := stackPtr+1;
    if endStackPtr > maxDigits + 1 then
      endStackPtr := 1;
 { now, all digits are calculated using trunc(d*10^(-n)-int(d*10^(-n-1))*10) }
    corrVal := 0.0;
 { the power of 10 with which the resulting string has to be "multiplied" }
 { if the decimal point is placed after the first significant digit       }
    correct := digits-1;
{$ifdef DEBUG_NASM}
    writeln(stderr,'endStackPtr = ',endStackPtr);
{$endif DEBUG_NASM}
    repeat
      if (currprec > 0) then
        begin
          intPart:= trunc(intPartStack[stackPtr]-corrVal);
          dec(currPrec);
          inc(spos);
          temp[spos] := chr(intPart+ord('0'));
{$ifdef DEBUG_NASM}
    writeln(stderr,'stackptr =',stackptr,' intpart = ',intpart);
{$endif DEBUG_NASM}
          if temp[spos] > '9' then
            begin
              temp[spos] := chr(ord(temp[spos])-10);
              roundStr(temp,spos-1);
            end;
        end;
      corrVal := int(intPartStack[stackPtr]) * 10.0;
{$ifdef DEBUG_NASM}
    writeln(stderr,'trunc(corrval) = ',trunc(corrval));
{$endif DEBUG_NASM}
      dec(stackPtr);
      if stackPtr = 0 then
        stackPtr := maxDigits+1;
    until (overflow and (stackPtr = endStackPtr)) or
          (not overflow and (stackPtr = maxDigits+1)) or (currPrec = 0);
    { round if we didn't use all available digits yet and if the }
    { remainder is > 5                                           }
    if (overflow or
        (stackPtr < maxDigits+1)) then
      begin
        { we didn't use all available digits of the whole part -> make sure }
        { the fractional part is not used for rounding later                }
        currprec := -1;
        { instead, round based on the next whole digit }
        if (int(intPartStack[stackPtr]-corrVal) >= 5.0) then
           roundStr(temp,spos);
        end;
{$ifdef DEBUG_NASM}
    writeln(stderr,'temp at getintpart exit is = ',temp);
{$endif DEBUG_NASM}
  end;

begin
  case real_type of
    rt_s32real :
      begin
         maxlen:=16;
         minlen:=8;
         explen:=4;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 1.1920928955e-07;
      end;
    rt_s64real :
      begin
         maxlen := 22;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 2.2204460493e-16;
         minlen:=9;
         explen:=5;
      end;
    rt_s80real,
    rt_sc80real:
      begin
         { Different in TP help, but this way the output is the same (JM) }
         maxlen:=25;
         minlen:=10;
         explen:=6;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 1.0842021725e-19;
      end;
    rt_c64bit  :
      begin
         maxlen:=23;
         minlen:=10;
         { according to TP (was 5) (FK) }
         explen:=6;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 2.2204460493e-16;
      end;
    rt_currency :
      begin
         { Different in TP help, but this way the output is the same (JM) }
         maxlen:=25;
         minlen:=10;
         explen:=0;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 1.0842021725e-19;
      end;
    rt_s128real  :
      begin
         { Different in TP help, but this way the output is the same (JM) }
         maxlen:=25;
         minlen:=10;
         explen:=6;
         { correction used with comparing to avoid rounding/precision errors }
         roundCorr := 1.0842021725e-19;
      end;
    end;
  { check parameters }
  { default value for length is -32767 }
  if len=-32767 then
    len:=maxlen;
  { determine sign. before precision, needs 2 less calls to abs() }
{$ifndef endian_big}
{$ifdef SUPPORT_EXTENDED}
  { extended, format (MSB): 1 Sign bit, 15 bit exponent, 64 bit mantissa }
  sign := (TSplitExtended(d).w and $8000) <> 0;
  expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  fraczero := (TSplitExtended(d).cards[0] = 0) and
                  ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
{$else SUPPORT_EXTENDED}
{$ifdef SUPPORT_DOUBLE}
{$ifdef FPC_DOUBLE_HILO_SWAPPED}
  { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  { high and low dword are swapped when using the arm fpa }
  sign := ((TSplitDouble(d).cards[0] shr 20) and $800) <> 0;
  expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
              (TSplitDouble(d).cards[1] = 0);
{$else FPC_DOUBLE_HILO_SWAPPED}
  { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
              (TSplitDouble(d).cards[0] = 0);
{$endif FPC_DOUBLE_HILO_SWAPPED}
{$else SUPPORT_DOUBLE}
{$ifdef SUPPORT_SINGLE}
  { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  fraczero := (TSplitSingle(d).cards[0] and $7fffff = 0);
{$else SUPPORT_SINGLE}
  {$error No little endian floating type supported yet in real2str}
{$endif SUPPORT_SINGLE}
{$endif SUPPORT_DOUBLE}
{$endif SUPPORT_EXTENDED}
{$else endian_big}
{$ifdef SUPPORT_EXTENDED}
  {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
{$else SUPPORT_EXTENDED}
{$ifdef SUPPORT_DOUBLE}
  { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  sign := ((TSplitDouble(d).cards[0] shr 20) and $800) <> 0;
  expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
              (TSplitDouble(d).cards[1] = 0);
{$else SUPPORT_DOUBLE}
{$ifdef SUPPORT_SINGLE}
  { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  sign := ((TSplitSingle(d).bytes[0] and $80)) <> 0;
  expMaximal := ((TSplitSingle(d).words[0] shr 7) and $ff) = 255;
  fraczero:= (TSplitSingle(d).cards[0] and $7fffff = 0);
{$else SUPPORT_SINGLE}
  {$error No big endian floating type supported yet in real2str}
{$endif SUPPORT_SINGLE}
{$endif SUPPORT_DOUBLE}
{$endif SUPPORT_EXTENDED}
{$endif endian}
  if expMaximal then
    if fraczero then
      if sign then
        temp := '-Inf'
      else temp := '+Inf'
    else temp := 'Nan'
  else
    begin
      {  d:=abs(d); this converts d to double so we loose precision }
      { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
      if sign then
        d:=-d;
      { determine precision : maximal precision is : }
      currPrec := maxlen-explen-2;
      { this is also the maximal number of decimals !!}
      if f>currprec then
        f:=currprec;
      { when doing a fixed-point, we need less characters.}
      if (f<0) {or ((d<>0) and ((d>maxexp) and (d>minexp)))} then
        begin
        { determine maximal number of decimals }
          if (len>=0) and (len<minlen) then
            len:=minlen;
          if (len>0) and (len<maxlen) then
            currprec:=len-explen-2;
        end;

      { leading zero, may be necessary for things like str(9.999:0:2) to }
      { be able to insert an extra character at the start of the string  }
      temp := ' 0';
      { position in the temporary output string }
      spos := 2;
      { get the integer part }
      correct := 0;
      GetIntPart(d);
      { now process the fractional part }
      if d > 1.0- roundCorr then
        d := frac(d);
      { if we have to round earlier than the amount of available precision, }
      { only calculate digits up to that point                              }
      if (f >= 0) and (currPrec > f) then
        currPrec := f;
      { if integer part was zero, go to the first significant digit of the }
      { fractional part                                                    }
      { make sure we don't get an endless loop if d = 0                    }
      if (spos = 2) and (d <> 0.0) then
        begin
         { take rounding errors into account }
          while d < 0.1-roundCorr do
            begin
              d := d * 10.0;
              dec(correct);
              { adjust the precision depending on how many digits we  }
              { already "processed" by multiplying by 10, but only if }
              { the amount of precision is specified                  }
              if f >= 0 then
                dec(currPrec);
            end;
          dec(correct);
        end;
      { current length of the output string in endPos }
      endPos := spos;
      { always calculate at least 1 fractional digit for rounding }
      if (currPrec >= 0) then
        begin
          corrVal := 0.5;
          factor := 1;
          for fracCount := 1 to currPrec do
            factor := factor * 10.0;
          corrval := corrval / factor;
          { for single, we may write more significant digits than are available,
            so the rounding correction itself can show up -> don't round in that
            case
          }
          if real_type<>rt_s32real then
            d:=d+d*roundCorr;
          if d >= corrVal then
            d := d + corrVal;
          if int(d) = 1 then
            begin
              roundStr(temp,spos);
              d := frac(d);
            end;
          { calculate the necessary fractional digits }
          for fracCount := 1 to currPrec do
            begin
              if d > 1.0 then
                d := frac(d) * 10.0
              else d := d * 10.0;
              inc(spos);
              temp[spos] := chr(trunc(d)+ord('0'));
              if temp[spos] > '9' then
                { possible because trunc and the "*10.0" aren't exact :( }
                begin
                  temp[spos] := chr(ord(temp[spos]) - 10);
                  roundStr(temp,spos-1);
                end;
            end;
          { new length of string }
          endPos := spos;
        end;
      setLength(temp,endPos);
      { delete leading zero if we didn't need it while rounding at the }
      { string level                                                   }
      if temp[2] = '0' then
        delete(temp,2,1)
      { the rounding caused an overflow to the next power of 10 }
      else inc(correct);
      if sign then
        temp[1] := '-';
      if (f<0) or (correct>(round(ln(maxexp)/ln(10)))) then
        begin
          insert ('.',temp,3);
          str(abs(correct),power);
          if length(power)<explen-2 then
            power:=copy(zero,1,explen-2-length(power))+power;
          if correct<0 then
            power:='-'+power
          else
            power:='+'+power;
          temp:=temp+'E'+power;
        end
      else
        begin
          if not sign then
            begin
              delete(temp,1,1);
              dot := 2
            end
          else
            dot := 3;
          { set zeroes and dot }
          if correct>=0 then
            begin
              if length(temp)<correct+dot+f-1 then
                temp:=temp+copy(zero,1,correct+dot+f-length(temp));
              insert ('.',temp,correct+dot);
            end
          else
            begin
              correct:=abs(correct);
              insert(copy(zero,1,correct),temp,dot-1);
              insert ('.',temp,dot);
            end;
          { correct length to fit precision }
          if f>0 then
            setlength(temp,pos('.',temp)+f)
          else
            setLength(temp,pos('.',temp)-1);
        end;
    end;
    if length(temp)<len then
      s:=space(len-length(temp))+temp
    else s:=temp;
end;


Procedure str_real_iso (len,f : longint; d : ValReal; real_type :treal_type; out s : string);
  var
   i : Integer;
  begin
    str_real(len,f,d,real_type,s);
    for i:=1 to Length(s) do
      if s[i]='E' then
        s[i]:='e';
  end;