Commit 005c6731539d17cbf18dbe813d2003c8fbdb98d1

Thomas de Grivel 2024-11-21T04:09:34

fix floats again, make some new casts

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index a72179c..4c6ee24 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -1938,7 +1938,7 @@ sw buf_inspect_f128_size (s_pretty *pretty, const f128 *x)
         return r;
       result += r;
     }
-    if ((r = buf_inspect_s64_size(pretty, &exp)) <= 0)
+    if ((r = buf_inspect_s64_decimal_size(pretty, &exp)) <= 0)
       return r;
     result += r;
   }
diff --git a/libkc3/s.c.in b/libkc3/s.c.in
index 9e13162..70d4a33 100644
--- a/libkc3/s.c.in
+++ b/libkc3/s.c.in
@@ -71,9 +71,7 @@ s_bits$ * s_bits$_init_cast
     *s = (s_bits$) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! s_bits$_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return s_bits$_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (s_bits$) tag->data.u8;
     return s;
diff --git a/libkc3/s16.c b/libkc3/s16.c
index d3339ca..37f1caf 100644
--- a/libkc3/s16.c
+++ b/libkc3/s16.c
@@ -71,9 +71,7 @@ s16 * s16_init_cast
     *s = (s16) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! s16_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return s16_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (s16) tag->data.u8;
     return s;
diff --git a/libkc3/s32.c b/libkc3/s32.c
index ce50e80..9f6426a 100644
--- a/libkc3/s32.c
+++ b/libkc3/s32.c
@@ -71,9 +71,7 @@ s32 * s32_init_cast
     *s = (s32) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! s32_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return s32_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (s32) tag->data.u8;
     return s;
diff --git a/libkc3/s64.c b/libkc3/s64.c
index 24ac7d2..dde01ae 100644
--- a/libkc3/s64.c
+++ b/libkc3/s64.c
@@ -71,9 +71,7 @@ s64 * s64_init_cast
     *s = (s64) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! s64_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return s64_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (s64) tag->data.u8;
     return s;
diff --git a/libkc3/s8.c b/libkc3/s8.c
index 29e7fad..3b7c008 100644
--- a/libkc3/s8.c
+++ b/libkc3/s8.c
@@ -71,9 +71,7 @@ s8 * s8_init_cast
     *s = (s8) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! s8_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return s8_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (s8) tag->data.u8;
     return s;
diff --git a/libkc3/str.c b/libkc3/str.c
index 74903d2..8705bd3 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -373,6 +373,12 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
     return str_init_callable(str, tag->data.callable);
   case TAG_CHARACTER:
     return str_init_character(str, tag->data.character);
+  case TAG_F32:
+    return str_init_f32(str, tag->data.f32);
+  case TAG_F64:
+    return str_init_f64(str, tag->data.f64);
+  case TAG_F128:
+    return str_init_f128(str, tag->data.f128);
   case TAG_IDENT:
     return str_init_ident(str, &tag->data.ident);
   case TAG_LIST:
@@ -555,6 +561,189 @@ s_str * str_init_f (s_str *str, const char *fmt, ...)
   return str;
 }
 
+s_str * str_init_f32 (s_str *str, f32 x)
+{
+  char a[32];
+  s_buf buf;
+  s64 exp;
+  u8 i;
+  u8 j;
+  sw r;
+  buf_init(&buf, false, sizeof(a), a);
+  exp = 0.0;
+  if (x == 0.0) {
+    if ((r = buf_write_1(&buf, "0.0")) < 0)
+      return NULL;
+    goto ok;
+  }
+  if (x < 0) {
+    if ((r = buf_write_1(&buf, "-")) <= 0)
+      return NULL;
+    x = -x;
+  }
+  if (x >= 1.0)
+    while (x >= 10.0) {
+      x /= 10.0;
+      exp++;
+    }
+  else
+    while (x < 1.0) {
+      x *= 10.0;
+      exp--;
+    }
+  i = (u8) x;
+  x -= i;
+  i += '0';
+  if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+    return NULL;
+  if ((r = buf_write_1(&buf, ".")) <= 0)
+    return NULL;
+  j = 6;
+  do {
+    x *= 10;
+    i = (u8) x;
+    x -= i;
+    i += '0';
+    if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+      return NULL;
+    j--;
+  } while (x > pow(0.1, j) && j);
+  if (exp) {
+    if ((r = buf_write_1(&buf, "e")) <= 0)
+      return NULL;
+    if (exp > 0) {
+      if ((r = buf_write_1(&buf, "+")) <= 0)
+        return NULL;
+    }
+    if ((r = buf_inspect_s64_decimal(&buf, &exp)) <= 0)
+      return NULL;
+  }
+ ok:
+  return buf_read_to_str(&buf, str);
+}
+
+s_str * str_init_f64 (s_str *str, f64 x)
+{
+  char a[32];
+  s_buf buf;
+  s64 exp;
+  u8 i;
+  u8 j;
+  sw r;
+  buf_init(&buf, false, sizeof(a), a);
+  exp = 0.0;
+  if (x == 0.0) {
+    if ((r = buf_write_1(&buf, "0.0")) < 0)
+      return NULL;
+    goto ok;
+  }
+  if (x < 0) {
+    if ((r = buf_write_1(&buf, "-")) <= 0)
+      return NULL;
+    x = -x;
+  }
+  if (x >= 1.0)
+    while (x >= 10.0) {
+      x /= 10.0;
+      exp++;
+    }
+  else
+    while (x < 1.0) {
+      x *= 10.0;
+      exp--;
+    }
+  i = (u8) x;
+  x -= i;
+  i += '0';
+  if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+    return NULL;
+  if ((r = buf_write_1(&buf, ".")) <= 0)
+    return NULL;
+  j = 14;
+  do {
+    x *= 10;
+    i = (u8) x;
+    x -= i;
+    i += '0';
+    if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+      return NULL;
+    j--;
+  } while (x > pow(0.1, j) && j);
+  if (exp) {
+    if ((r = buf_write_1(&buf, "e")) <= 0)
+      return NULL;
+    if (exp > 0) {
+      if ((r = buf_write_1(&buf, "+")) <= 0)
+        return NULL;
+    }
+    if ((r = buf_inspect_s64_decimal(&buf, &exp)) <= 0)
+      return NULL;
+  }
+ ok:
+  return buf_read_to_str(&buf, str);
+}
+
+s_str * str_init_f128 (s_str *str, f128 x)
+{
+  char a[128];
+  s_buf buf;
+  s64 exp;
+  u8 i;
+  u8 j;
+  sw r;
+  buf_init(&buf, false, sizeof(a), a);
+  exp = 0.0;
+  if (x == 0.0) {
+    if ((r = buf_write_1(&buf, "0.0")) < 0)
+      return NULL;
+    goto ok;
+  }
+  if (x < 0) {
+    if ((r = buf_write_1(&buf, "-")) <= 0)
+      return NULL;
+    x = -x;
+  }
+  if (x >= 1.0)
+    while (x >= 10.0) {
+      x /= 10.0;
+      exp++;
+    }
+  else
+    while (x < 1.0) {
+      x *= 10.0;
+      exp--;
+    }
+  i = (u8) x;
+  x -= i;
+  i += '0';
+  if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+    return NULL;
+  if ((r = buf_write_1(&buf, ".")) <= 0)
+    return NULL;
+  j = 33;
+  do {
+    x *= 10;
+    i = (u8) x;
+    x -= i;
+    i += '0';
+    if ((r = buf_write_character_utf8(&buf, i)) <= 0)
+      return NULL;
+    j--;
+  } while (x > pow(0.1, j) && j);
+  if (exp) {
+    if ((r = buf_write_1(&buf, "e")) <= 0)
+      return NULL;
+    if (exp > 0) {
+      if ((r = buf_write_1(&buf, "+")) <= 0)
+        return NULL;
+    }
+    if ((r = buf_inspect_s64_decimal(&buf, &exp)) <= 0)
+      return NULL;
+  }
+ ok:
+  return buf_read_to_str(&buf, str);
+}
+
 DEF_STR_INIT_STRUCT(fn)
 
 s_str * str_init_ftime (s_str *str, s_time *time, const s_str *format)
diff --git a/libkc3/sw.c b/libkc3/sw.c
index 38ba225..ed966fa 100644
--- a/libkc3/sw.c
+++ b/libkc3/sw.c
@@ -71,9 +71,7 @@ sw * sw_init_cast
     *s = (sw) tag->data.s8;
     return s;
   case TAG_STR:
-    if (! sw_init_str(s, &tag->data.str))
-      return NULL;
-    return s;
+    return sw_init_str(s, &tag->data.str);
   case TAG_U8:
     *s = (sw) tag->data.u8;
     return s;
diff --git a/libkc3/u.c.in b/libkc3/u.c.in
index 4aae172..88c320a 100644
--- a/libkc3/u.c.in
+++ b/libkc3/u.c.in
@@ -54,20 +54,22 @@ u_bits$ * u_bits$_init_cast
   case TAG_RATIO:
     *u = ratio_to_u_bits$(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (u_bits$) tag->data.sw;
+  case TAG_S8:
+    *u = (u_bits$) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (u_bits$) tag->data.s64;
+  case TAG_S16:
+    *u = (u_bits$) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (u_bits$) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (u_bits$) tag->data.s16;
+  case TAG_S64:
+    *u = (u_bits$) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (u_bits$) tag->data.s8;
+  case TAG_STR:
+    return u_bits$_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (u_bits$) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (u_bits$) tag->data.u8;
diff --git a/libkc3/u16.c b/libkc3/u16.c
index b093681..43ea2af 100644
--- a/libkc3/u16.c
+++ b/libkc3/u16.c
@@ -54,20 +54,22 @@ u16 * u16_init_cast
   case TAG_RATIO:
     *u = ratio_to_u16(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (u16) tag->data.sw;
+  case TAG_S8:
+    *u = (u16) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (u16) tag->data.s64;
+  case TAG_S16:
+    *u = (u16) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (u16) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (u16) tag->data.s16;
+  case TAG_S64:
+    *u = (u16) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (u16) tag->data.s8;
+  case TAG_STR:
+    return u16_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (u16) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (u16) tag->data.u8;
diff --git a/libkc3/u32.c b/libkc3/u32.c
index d10e282..5a4dc6d 100644
--- a/libkc3/u32.c
+++ b/libkc3/u32.c
@@ -54,20 +54,22 @@ u32 * u32_init_cast
   case TAG_RATIO:
     *u = ratio_to_u32(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (u32) tag->data.sw;
+  case TAG_S8:
+    *u = (u32) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (u32) tag->data.s64;
+  case TAG_S16:
+    *u = (u32) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (u32) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (u32) tag->data.s16;
+  case TAG_S64:
+    *u = (u32) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (u32) tag->data.s8;
+  case TAG_STR:
+    return u32_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (u32) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (u32) tag->data.u8;
diff --git a/libkc3/u64.c b/libkc3/u64.c
index c909495..c7d6c26 100644
--- a/libkc3/u64.c
+++ b/libkc3/u64.c
@@ -54,20 +54,22 @@ u64 * u64_init_cast
   case TAG_RATIO:
     *u = ratio_to_u64(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (u64) tag->data.sw;
+  case TAG_S8:
+    *u = (u64) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (u64) tag->data.s64;
+  case TAG_S16:
+    *u = (u64) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (u64) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (u64) tag->data.s16;
+  case TAG_S64:
+    *u = (u64) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (u64) tag->data.s8;
+  case TAG_STR:
+    return u64_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (u64) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (u64) tag->data.u8;
diff --git a/libkc3/u8.c b/libkc3/u8.c
index 9f5a414..4de42ac 100644
--- a/libkc3/u8.c
+++ b/libkc3/u8.c
@@ -54,20 +54,22 @@ u8 * u8_init_cast
   case TAG_RATIO:
     *u = ratio_to_u8(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (u8) tag->data.sw;
+  case TAG_S8:
+    *u = (u8) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (u8) tag->data.s64;
+  case TAG_S16:
+    *u = (u8) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (u8) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (u8) tag->data.s16;
+  case TAG_S64:
+    *u = (u8) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (u8) tag->data.s8;
+  case TAG_STR:
+    return u8_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (u8) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (u8) tag->data.u8;
diff --git a/libkc3/uw.c b/libkc3/uw.c
index 3db6bab..8230885 100644
--- a/libkc3/uw.c
+++ b/libkc3/uw.c
@@ -54,20 +54,22 @@ uw * uw_init_cast
   case TAG_RATIO:
     *u = ratio_to_uw(&tag->data.ratio);
     return u;
-  case TAG_SW:
-    *u = (uw) tag->data.sw;
+  case TAG_S8:
+    *u = (uw) tag->data.s8;
     return u;
-  case TAG_S64:
-    *u = (uw) tag->data.s64;
+  case TAG_S16:
+    *u = (uw) tag->data.s16;
     return u;
   case TAG_S32:
     *u = (uw) tag->data.s32;
     return u;
-  case TAG_S16:
-    *u = (uw) tag->data.s16;
+  case TAG_S64:
+    *u = (uw) tag->data.s64;
     return u;
-  case TAG_S8:
-    *u = (uw) tag->data.s8;
+  case TAG_STR:
+    return uw_init_str(u, &tag->data.str);
+  case TAG_SW:
+    *u = (uw) tag->data.sw;
     return u;
   case TAG_U8:
     *u = (uw) tag->data.u8;