Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/json/jsonf.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / json / jsonf.om` 710 lines ยท 13345 bytes Raw Blame History 1 note seed/json/jsonf.om: OPTIONAL exact float conversion for the JSON units (omega-mv #788). Needs json.om and 2 note jsonw.om before it. The reader never converts numbers; this unit is only for callers that want an f64. 3 note js_f64 is decimal-to-binary64 with round-to-nearest-even, exact for every lexeme (overflow gives 4 note +-infinity, underflow +-0). js_fmt64 / js_wf64 write the shortest digits that convert back to the same 5 note f64, in Python repr layout. Method: the arbitrary-precision decimal of Go strconv (decimal.go, 6 note atof.go floatBits, ftoa.go roundShortest): 800 digits plus a truncation flag, exact shifts by powers of 7 note two. It is NOT the serde_json default float parser, which is not correctly rounded. 8 note Scratch: a caller `long` array of jsfs cells. Cells 0..2 digit counts, 3..5 decimal points, 6..8 9 note truncation flags of decimals 0..2, 9 sign, 10 status (0 exact path, 1 overflow to infinity, 2 not a 10 note number lexeme), 11 formatted length; digits of decimal q at 16 + 800q; formatted bytes at 2416. 11 12 const jsfs ulong 2464 13 const jsfdig ulong 16 14 const jsfout ulong 2416 15 16 note ======== decimal digits ======== 17 18 fn js_dget fs edit long jsfs q ulong i ulong to ulong 19 ret ulong get fs add add jsfdig mul q 800 i 20 end 21 22 note js_dput: store digit v at w, or remember a lost nonzero digit beyond 800 23 fn js_dput fs edit long jsfs q ulong w ulong v ulong to void 24 if lt w 800 25 put fs add add jsfdig mul q 800 w long v 26 ret 27 end 28 if ne v 0 29 put fs add q 6 1 30 end 31 end 32 33 fn js_dtrim fs edit long jsfs q ulong to void 34 nd = ulong get fs q 35 while gt nd 0 bound 1000 36 if ne js_dget fs q sub nd 1 0 37 break 38 end 39 nd = sub nd 1 40 end 41 put fs q long nd 42 if eq nd 0 43 put fs add q 3 0 44 end 45 end 46 47 fn js_dassign fs edit long jsfs q ulong v ulong to void 48 tmp = new long 20 0 49 k = ulong 0 50 x = v 51 while gt x 0 bound 21 52 put tmp k long rem x 10 53 k = add k 1 54 x = div x 10 55 end 56 for i ulong k 57 js_dput fs q i ulong get tmp sub sub k 1 i 58 end 59 put fs q long k 60 put fs add q 3 long k 61 put fs add q 6 0 62 js_dtrim fs q 63 end 64 65 note js_drsh: divide decimal q by 2^k, 1 <= k <= 60 (Go rightShift); 9 * 2^60 + 2^60 < 2^64 66 fn js_drsh fs edit long jsfs q ulong k ulong to void 67 nd = ulong get fs q 68 r = ulong 0 69 n = ulong 0 70 while eq shr n k 0 bound 1000 71 if ge r nd 72 if eq n 0 73 put fs q 0 74 ret 75 end 76 while eq shr n k 0 bound 100 77 n = mul n 10 78 r = add r 1 79 end 80 break 81 end 82 n = add mul n 10 js_dget fs q r 83 r = add r 1 84 end 85 put fs add q 3 sub get fs add q 3 sub long r 1 86 js_drsh2 fs q k r n 87 end 88 89 fn js_drsh2 fs edit long jsfs q ulong k ulong r ulong n ulong to void 90 nd = ulong get fs q 91 mask = sub shl ulong 1 k 1 92 w = ulong 0 93 x = n 94 y = r 95 while lt y nd bound 1000 96 c = js_dget fs q y 97 js_dput fs q w shr x k 98 w = add w 1 99 x = add mul and x mask 10 c 100 y = add y 1 101 end 102 while gt x 0 bound 1000 103 js_dput fs q w shr x k 104 if lt w 800 105 w = add w 1 106 end 107 x = mul and x mask 10 108 end 109 put fs q long w 110 js_dtrim fs q 111 end 112 113 note js_dlsh: multiply decimal q by 2^k, 1 <= k <= 60 (Go leftShift). The first pass finds the number of new 114 note leading digits from the final carry instead of Go's leftcheats table. 115 fn js_dlsh fs edit long jsfs q ulong k ulong to void 116 nd = ulong get fs q 117 n = ulong 0 118 r = nd 119 while gt r 0 bound 1000 120 r = sub r 1 121 n = div add n shl js_dget fs q r k 10 122 end 123 del = ulong 0 124 while gt n 0 bound 100 125 del = add del 1 126 n = div n 10 127 end 128 js_dlsh2 fs q k del 129 nd = add nd del 130 if gt nd 800 131 nd = 800 132 end 133 put fs q long nd 134 put fs add q 3 add get fs add q 3 long del 135 js_dtrim fs q 136 end 137 138 fn js_dlsh2 fs edit long jsfs q ulong k ulong del ulong to void 139 r = ulong get fs q 140 w = add r del 141 n = ulong 0 142 while gt r 0 bound 1000 143 r = sub r 1 144 n = add n shl js_dget fs q r k 145 w = sub w 1 146 js_dput fs q w rem n 10 147 n = div n 10 148 end 149 while gt n 0 bound 100 150 w = sub w 1 151 js_dput fs q w rem n 10 152 n = div n 10 153 end 154 end 155 156 fn js_dshift fs edit long jsfs q ulong k long to void 157 if eq get fs q 0 158 ret 159 end 160 s = k 161 while gt s 60 bound 100 162 js_dlsh fs q 60 163 s = sub s 60 164 end 165 if gt s 0 166 js_dlsh fs q ulong s 167 end 168 while lt s -60 bound 100 169 js_drsh fs q 60 170 s = add s 60 171 end 172 if lt s 0 173 js_drsh fs q ulong neg s 174 end 175 end 176 177 note js_dsru: Go shouldRoundUp at digit index i; an exact half rounds to even unless digits were lost 178 fn js_dsru fs edit long jsfs q ulong i long to bool 179 nd = get fs q 180 if or lt i 0 ge i nd 181 ret false 182 end 183 d = js_dget fs q ulong i 184 if and eq d 5 eq add i 1 nd 185 if ne get fs add q 6 0 186 ret true 187 end 188 if eq i 0 189 ret false 190 end 191 ret eq rem js_dget fs q ulong sub i 1 2 1 192 end 193 ret ge d 5 194 end 195 196 fn js_drint fs edit long jsfs q ulong to ulong 197 dp = get fs add q 3 198 if gt dp 20 199 ret `18446744073709551615` 200 end 201 nd = get fs q 202 n = ulong 0 203 i = long 0 204 while and lt i dp lt i nd bound 100 205 n = add mul n 10 js_dget fs q ulong i 206 i = add i 1 207 end 208 while lt i dp bound 100 209 n = mul n 10 210 i = add i 1 211 end 212 if js_dsru fs q dp 213 n = add n 1 214 end 215 ret n 216 end 217 218 fn js_drdown fs edit long jsfs q ulong i long to void 219 if or lt i 0 ge i get fs q 220 ret 221 end 222 put fs q i 223 js_dtrim fs q 224 end 225 226 fn js_drup fs edit long jsfs q ulong i long to void 227 if or lt i 0 ge i get fs q 228 ret 229 end 230 j = i 231 while gt j 0 bound 1000 232 j = sub j 1 233 d = js_dget fs q ulong j 234 if lt d 9 235 js_dput fs q ulong j add d 1 236 put fs q add j 1 237 ret 238 end 239 end 240 js_dput fs q 0 1 241 put fs q 1 242 put fs add q 3 add get fs add q 3 1 243 end 244 245 fn js_dround fs edit long jsfs q ulong i long to void 246 if or lt i 0 ge i get fs q 247 ret 248 end 249 if js_dsru fs q i 250 js_drup fs q i 251 ret 252 end 253 js_drdown fs q i 254 end 255 256 note ======== lexeme to f64 ======== 257 258 note js_dmant: digits of the mantissa into decimal 0. cnt counts every significant digit, also those past 259 note the 800 kept, so the decimal point stays exact; a lost nonzero digit sets the truncation flag. 260 fn js_dmant fs edit long jsfs src view uchar * p ulong n ulong to ulong 261 q = p 262 dot = false 263 cnt = ulong 0 264 dp = long 0 265 while lt q n bound `9223372036854775807` 266 c = get src q 267 if eq c 46 268 if dot 269 break 270 end 271 dot = true 272 dp = long cnt 273 q = add q 1 274 continue 275 end 276 if or lt c 48 gt c 57 277 break 278 end 279 if and eq c 48 eq cnt 0 280 dp = sub dp 1 281 q = add q 1 282 continue 283 end 284 if lt cnt 800 285 js_dput fs 0 cnt ulong sub c 48 286 else 287 if ne c 48 288 put fs 6 1 289 end 290 end 291 cnt = add cnt 1 292 q = add q 1 293 end 294 if not dot 295 dp = long cnt 296 end 297 nd = cnt 298 if gt nd 800 299 nd = 800 300 end 301 put fs 0 long nd 302 put fs 3 dp 303 ret q 304 end 305 306 fn js_dexp fs edit long jsfs src view uchar * p ulong n ulong to void 307 q = p 308 if not or js_isb src q n 101 js_isb src q n 69 309 ret 310 end 311 q = add q 1 312 ng = false 313 if js_isb src q n 43 314 q = add q 1 315 else 316 if js_isb src q n 45 317 ng = true 318 q = add q 1 319 end 320 end 321 note The mantissa displacement is exact. Once |exponent| reaches |dp| + 331, its sign 322 note decides the binary64 range even after cancellation (js_dbits uses dp > 310 or dp < -330). 323 note Saturate only there, not at a fixed exponent; input span <= jsmaxn + 2 keeps this in long. 324 lim = get fs 3 325 if lt lim 0 326 lim = neg lim 327 end 328 lim = add lim 331 329 e = long 0 330 while lt q n bound `9223372036854775807` 331 c = get src q 332 if or lt c 48 gt c 57 333 break 334 end 335 d = long sub c 48 336 if gt e div sub lim d 10 337 e = lim 338 else 339 e = add mul e 10 d 340 end 341 q = add q 1 342 end 343 if ng 344 e = neg e 345 end 346 put fs 3 add get fs 3 e 347 end 348 349 fn js_fpack fs edit long jsfs m ulong f ulong to ulong 350 b = or m shl f 52 351 if ne get fs 9 0 352 b = or b shl ulong 1 63 353 end 354 ret b 355 end 356 357 fn js_fovf fs edit long jsfs to ulong 358 put fs 10 1 359 ret js_fpack fs 0 2047 360 end 361 362 fn js_powtab n long to long 363 if ge n 9 364 ret 27 365 end 366 if eq n 0 367 ret 1 368 end 369 if eq n 1 370 ret 3 371 end 372 if eq n 2 373 ret 6 374 end 375 if eq n 3 376 ret 9 377 end 378 if eq n 4 379 ret 13 380 end 381 if eq n 5 382 ret 16 383 end 384 if eq n 6 385 ret 19 386 end 387 if eq n 7 388 ret 23 389 end 390 ret 26 391 end 392 393 fn js_dlow fs edit long jsfs to bool 394 if eq get fs 0 0 395 ret false 396 end 397 dp = get fs 3 398 if lt dp 0 399 ret true 400 end 401 ret and eq dp 0 lt get fs jsfdig 5 402 end 403 404 note js_dscale: scale decimal 0 into [0.5, 1) by powers of two; returns the binary exponent 405 fn js_dscale fs edit long jsfs to long 406 e = long 0 407 while gt get fs 3 0 bound 1000 408 s = js_powtab get fs 3 409 js_dshift fs 0 neg s 410 e = add e s 411 end 412 while js_dlow fs bound 1000 413 s = js_powtab neg get fs 3 414 js_dshift fs 0 s 415 e = sub e s 416 end 417 ret e 418 end 419 420 note js_dbits: Go floatBits for binary64 (bias -1023, 52 mantissa bits, 11 exponent bits) on decimal 0 421 fn js_dbits fs edit long jsfs to ulong 422 if eq get fs 0 0 423 ret js_fpack fs 0 0 424 end 425 dp = get fs 3 426 if gt dp 310 427 ret js_fovf fs 428 end 429 if lt dp -330 430 ret js_fpack fs 0 0 431 end 432 e = sub js_dscale fs 1 433 if lt e -1022 434 js_dshift fs 0 add e 1022 435 e = long -1022 436 end 437 if ge e 1024 438 ret js_fovf fs 439 end 440 js_dshift fs 0 53 441 m = js_drint fs 0 442 if eq m `9007199254740992` 443 m = shr m 1 444 e = add e 1 445 if ge e 1024 446 ret js_fovf fs 447 end 448 end 449 if eq and m `4503599627370496` 0 450 ret js_fpack fs m 0 451 end 452 ret js_fpack fs and m `4503599627370495` ulong add e 1023 453 end 454 455 note js_f64: the f64 nearest the number lexeme at src[off] (ties to even). Status js_f64st: 0 finite, 1 the 456 note magnitude overflowed to infinity, 2 no number lexeme at off (the result is then a quiet NaN). 457 fn js_f64 src view uchar * off ulong fs edit long jsfs to f64 458 for i ulong 12 459 put fs i 0 460 end 461 n = js_slim src off 462 if eq js_numend src off n 0 463 put fs 10 2 464 ret bitcast f64 ulong `9221120237041090560` 465 end 466 p = off 467 if js_atv src p 45 468 put fs 9 1 469 p = add p 1 470 end 471 p = js_dmant fs src p n 472 js_dexp fs src p n 473 js_dtrim fs 0 474 ret bitcast f64 js_dbits fs 475 end 476 477 fn js_f64st fs edit long jsfs to ulong 478 ret ulong get fs 10 479 end 480 481 note ======== shortest f64 text ======== 482 483 fn js_fdig fs edit long jsfs q ulong i long to long 484 if or lt i 0 ge i get fs q 485 ret 0 486 end 487 ret long js_dget fs q ulong i 488 end 489 490 fn js_fud ud ulong m long u long to ulong 491 if and eq ud 0 lt add m 1 u 492 ret 2 493 end 494 if and eq ud 0 ne m u 495 ret 1 496 end 497 if and eq ud 1 or ne m 9 ne u 0 498 ret 2 499 end 500 ret ud 501 end 502 503 fn js_fokd fs edit long jsfs inc bool ui long m long to bool 504 li = add sub ui get fs 4 get fs 5 505 if ne js_fdig fs 2 li m 506 ret true 507 end 508 ret and inc eq add li 1 get fs 2 509 end 510 511 note js_fstep: one digit of Go roundShortest; returns the new upperdelta 0..2, or 3 when d is final 512 fn js_fstep fs edit long jsfs inc bool ui long ud ulong to ulong 513 mi = add sub ui get fs 4 get fs 3 514 if ge mi get fs 0 515 ret 3 516 end 517 m = js_fdig fs 0 mi 518 d = js_fud ud m js_fdig fs 1 ui 519 okd = js_fokd fs inc ui m 520 oku = false 521 if gt d 0 522 oku = or inc or gt d 1 lt add ui 1 get fs 1 523 end 524 if and okd oku 525 js_dround fs 0 add mi 1 526 ret 3 527 end 528 if okd 529 js_drdown fs 0 add mi 1 530 ret 3 531 end 532 if oku 533 js_drup fs 0 add mi 1 534 ret 3 535 end 536 ret d 537 end 538 539 fn js_fwalk fs edit long jsfs inc bool to void 540 ud = ulong 0 541 ui = long 0 542 for k ulong 2000 543 r = js_fstep fs inc ui ud 544 if ge r 3 545 ret 546 end 547 ud = r 548 ui = add ui 1 549 end 550 end 551 552 note js_fshort: Go roundShortest on decimal 0 = mt * 2^(e-52); decimals 1 and 2 hold the upper and lower 553 note halfway bounds 554 fn js_fshort fs edit long jsfs mt ulong e long to void 555 if eq mt 0 556 put fs 0 0 557 ret 558 end 559 if gt e -1022 560 if ge mul 332 sub get fs 3 get fs 0 mul 100 sub e 52 561 ret 562 end 563 end 564 js_dassign fs 1 add mul mt 2 1 565 js_dshift fs 1 sub e 53 566 if or gt mt `4503599627370496` eq e -1022 567 js_dassign fs 2 add mul sub mt 1 2 1 568 js_dshift fs 2 sub e 53 569 else 570 js_dassign fs 2 add mul sub mul mt 2 1 2 1 571 js_dshift fs 2 sub e 54 572 end 573 js_fwalk fs eq and mt 1 0 574 end 575 576 fn js_fput fs edit long jsfs k ulong c ulong to ulong 577 put fs add jsfout k long c 578 ret add k 1 579 end 580 581 fn js_ffix fs edit long jsfs k ulong to ulong 582 w = k 583 dp = get fs 3 584 nd = get fs 0 585 if le dp 0 586 w = js_fput fs w 48 587 w = js_fput fs w 46 588 for i ulong ulong neg dp 589 w = js_fput fs w 48 590 end 591 for i ulong ulong nd 592 w = js_fput fs w add js_dget fs 0 i 48 593 end 594 ret w 595 end 596 if ge dp nd 597 for i ulong ulong nd 598 w = js_fput fs w add js_dget fs 0 i 48 599 end 600 for i ulong ulong sub dp nd 601 w = js_fput fs w 48 602 end 603 w = js_fput fs w 46 604 w = js_fput fs w 48 605 ret w 606 end 607 for i ulong ulong dp 608 w = js_fput fs w add js_dget fs 0 i 48 609 end 610 w = js_fput fs w 46 611 for i ulong ulong sub nd dp 612 w = js_fput fs w add js_dget fs 0 add ulong dp i 48 613 end 614 ret w 615 end 616 617 fn js_fsci fs edit long jsfs k ulong to ulong 618 w = js_fput fs k add js_dget fs 0 0 48 619 nd = ulong get fs 0 620 if gt nd 1 621 w = js_fput fs w 46 622 for i ulong sub nd 1 623 w = js_fput fs w add js_dget fs 0 add i 1 48 624 end 625 end 626 w = js_fput fs w 101 627 x = sub get fs 3 1 628 if lt x 0 629 w = js_fput fs w 45 630 x = neg x 631 else 632 w = js_fput fs w 43 633 end 634 if lt x 10 635 w = js_fput fs w 48 636 end 637 if ge x 100 638 w = js_fput fs w add ulong div x 100 48 639 end 640 if ge x 10 641 w = js_fput fs w add ulong rem div x 10 10 48 642 end 643 w = js_fput fs w add ulong rem x 10 48 644 ret w 645 end 646 647 note js_frepr: Python repr layout: fixed when the decimal exponent x is -4 <= x < 16 (".0" added to whole 648 note numbers), else d[.ddd]e+XX / e-XX with at least two exponent digits; zero is 0.0 or -0.0. 649 fn js_frepr fs edit long jsfs sg bool to ulong 650 k = ulong 0 651 if sg 652 k = js_fput fs k 45 653 end 654 if eq get fs 0 0 655 k = js_fput fs k 48 656 k = js_fput fs k 46 657 ret js_fput fs k 48 658 end 659 x = sub get fs 3 1 660 if and ge x -4 lt x 16 661 ret js_ffix fs k 662 end 663 ret js_fsci fs k 664 end 665 666 note js_fmt64: shortest round-trip text of a finite x at fs[jsfout ..]; returns its length, -1 for inf or NaN 667 fn js_fmt64 fs edit long jsfs x f64 to long 668 bits = bitcast ulong x 669 ex = and shr bits 52 2047 670 mt = and bits `4503599627370495` 671 put fs 11 0 672 if eq ex 2047 673 ret -1 674 end 675 if eq ex 0 676 ex = 1 677 else 678 mt = or mt `4503599627370496` 679 end 680 e = sub long ex 1023 681 js_dassign fs 0 mt 682 js_dshift fs 0 sub e 52 683 js_fshort fs mt e 684 k = js_frepr fs ne shr bits 63 0 685 put fs 11 long k 686 ret long k 687 end 688 689 fn js_fmtch fs edit long jsfs i ulong to uchar 690 ret uchar get fs add jsfout i 691 end 692 693 note js_wf64: write a finite f64 as its shortest text; inf and NaN are refused (jswenm), JSON has no form 694 fn js_wf64 ob edit uchar * x f64 fs edit long jsfs to void 695 if ne get ob jswerr 0 696 ret 697 end 698 k = js_fmt64 fs x 699 if lt k 0 700 js_wfail ob jswenm 701 ret 702 end 703 if not js_wpre ob 704 ret 705 end 706 for i ulong ulong k 707 js_wb ob js_fmtch fs i 708 end 709 js_wpost ob 710 end