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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Port of `lang_NN.py` (Norwegian Nynorsk).
//!
//! Shape: **self-contained**. `Num2Word_NN` subclasses `Num2Word_Base` but
//! defines no `high_numwords`/`mid_numwords`/`low_numwords`, so the guard in
//! `Num2Word_Base.__init__` never fires: `self.cards` is never built and
//! `self.MAXVAL` is never assigned. `to_cardinal` is overridden outright and
//! drives a private `_int_to_word` recursion. Consequently `cards`/`maxval`/
//! `merge` stay at their trait defaults here, and there is **no overflow
//! check** — arbitrarily large input is accepted and silently degraded (see
//! bug 5 below). No integer input can make this language raise.
//!
//! `Num2Word_NN` overrides all four in-scope methods (`to_cardinal`,
//! `to_ordinal`, `to_ordinal_num`, `to_year`), so nothing is inherited from
//! `Num2Word_Base` for the ported surface. In particular `title()` is never
//! reached: base's `to_cardinal` applies it, but NN's override does not, and
//! `is_title` is `False` anyway.
//!
//! `setup()` also sets `pointword = "point"`, used only by the float path
//! (`"." in n`). `Num2Word_NN` overrides `to_cardinal` outright and handles
//! non-integer input inline off `str(number)`, so it never reaches
//! `base.float2tuple`/`to_cardinal_float`; [`LangNn::to_cardinal_float`] ports
//! that inline branch directly. It is surfaced via the `pointword()` trait
//! method for the same reason.
//!
//! Because everything keys on `str(number)`, a float/Decimal takes the string
//! route for *every* value, whole ones included: `to_cardinal(5.0)` is
//! "fem point zero", never "fem" — Base's `int(value) == value` whole-value
//! shortcut does not exist here. [`LangNn::cardinal_float_entry`] pins that
//! full routing (and `year_float_entry` inherits it, matching `to_year` ==
//! `to_cardinal`); [`LangNn::ordinal_float_entry`] rides the same path and
//! appends "-de" (`to_ordinal(5.0)` == "fem point zero-de");
//! [`LangNn::ordinal_num_float_entry`] is `str(number) + "."`, so "5.0." and
//! even "1e+16." are real outputs. And because the route is the string form,
//! a value whose `str()` is *exponent notation* crashes `int()` — a Python
//! bug reproduced faithfully (see bug 9 below).
//!
//! # Faithfully reproduced Python bugs
//!
//! This is a port, not a rewrite. Every item below looks wrong for Norwegian
//! but is exactly what Python emits, and each is pinned by the frozen corpus:
//!
//! 1. **Zero is English.** `_int_to_word` opens with
//! `return self.ones[0] if self.ones[0] else "zero"`. `ones[0]` is `""` —
//! always falsy — so the conditional is dead and every zero renders as the
//! English "zero", never Nynorsk "null". Corpus: `0 -> "zero"`,
//! `to_ordinal(0) -> "zero-de"`.
//! 2. **No teens.** The `< 100` branch is a plain tens/ones split with no
//! 11..19 special case, so 11 is "ti ein" (literally "ten one") instead of
//! "elleve", 12 is "ti to", 19 is "ti ni". This propagates upward:
//! `12345 -> "ti to tusen tre hundre førti fem"`.
//! 3. **No "hundre"/"tusen" elision and no compounding.** Norwegian writes
//! "hundre" for 100 and compounds ("tjueein"); this module always prefixes
//! the multiplier and space-separates every token, so `100 -> "ein hundre"`
//! and `1000 -> "ein tusen"`.
//! 4. **"million" never pluralises.** The `< 10^9` branch appends the bare
//! singular, so `10^7 -> "ti million"`, not "ti millionar".
//! 5. **Everything >= 10^9 degrades to digits.** The final `else` is
//! `return str(number) # Fallback for very large numbers` — no billion
//! word exists. So `10^9 -> "1000000000"` and, composed with the ordinal
//! suffix, `to_ordinal(10**9) -> "1000000000-de"`. This is the one place
//! where BigInt is load-bearing: the corpus reaches 10^21, well past u64,
//! and the value must round-trip as decimal digits. See [`int_to_word`].
//! 6. **The ordinal is cardinal + "-de", unconditionally.** No stem changes,
//! no agreement, and the suffix binds to the last token only, so
//! `to_ordinal(1234567)` ends "...seksti sju-de". Negatives are *not*
//! rejected (base's `errmsg_negord` guard is bypassed by the override):
//! `to_ordinal(-1) -> "minus ein-de"`.
//! 7. **An unknown currency code silently becomes kroner.** `to_currency`
//! looks the code up with `CURRENCY_FORMS.get(currency, <first value>)`
//! instead of indexing, so anything outside NOK/USD/EUR renders with NOK's
//! forms rather than raising: `to_currency(12.34, "JPY")` is
//! "ti to kroner tretti fire øre". The inherited `to_cheque` indexes the
//! same dict and *does* raise for those codes, so the two entry points
//! disagree about which currencies exist. Both halves are corpus-pinned.
//! 8. **Currency precision is hardcoded to two decimals.** `to_currency`
//! never reads `CURRENCY_PRECISION` (which is `{}` anyway), so the
//! zero-decimal and three-decimal currencies are wrong in both directions:
//! JPY grows a subunit and KWD/BHD are quantised to /100, not /1000.
//! 9. **Values whose `str()` uses exponent notation raise ValueError.** A
//! float at or above 1e16 reprs as `'1e+16'`; a Decimal whose exponent is
//! positive prints as `'1E+2'`/`'1E+20'` (and one whose adjusted exponent
//! drops below -6 as `'1E-7'`). None of those contain a "." — the whole
//! token hits `int()` and dies: `ValueError: invalid literal for int()
//! with base 10: '1e+16'`. Ones that *do* keep a "." (`'1.5e-05'`) split
//! and then die in the digit loop on the exponent marker (`int('e')`,
//! uppercase `int('E')` for Decimals). Corpus-pinned for 1e+16, 1e+20,
//! 1E+2 and 1E+20 across cardinal/ordinal/year. `to_ordinal_num` never
//! calls `int()`, so it *succeeds* on the same inputs ("1e+16.").
//! 10. **`"Infinity"`/`"NaN"` strings parse but blow up later.** Base's
//! `str_to_number` (`Decimal(value)`) accepts them; the failure is NN's
//! `int("Infinity")` inside `to_cardinal` (ValueError; "-Infinity" loses
//! its sign first, so the message always quotes 'Infinity') — while
//! `to_ordinal_num("Infinity")` is *answered*, with "Infinity.". The Rust
//! dispatcher hard-codes Base semantics for non-finite parses
//! (OverflowError), which NN never executes, so [`LangNn::str_to_number`]
//! punts them back to the pure-Python original via NotImplemented; the
//! fallback then raises (or returns) the byte-exact original per mode.
//!
//! # Dead code reproduced
//!
//! `_int_to_word`'s `number < 0` branch (`negword + _int_to_word(abs(n))`) is
//! unreachable: `to_cardinal` strips the sign textually before calling it, and
//! every recursive call passes a positive value. It is mirrored in
//! [`int_to_word`] to keep the structure aligned with the source, but it can
//! never fire. Note it would double the negword if it ever did.
use crate;
use crate;
use crateFloatValue;
use crate;
use BigInt;
use ;
use HashMap;
use FromStr;
/// `setup()`: note the trailing space — NN overrides base's `"(-) "`.
const NEGWORD: &str = "minus ";
const POINTWORD: &str = "point";
/// `self.ones`. Index 0 is `""`; see bug 1 — it is only ever read at index 0
/// through the dead conditional, and at 1..=9 for real digits.
const ONES: = ;
/// `self.tens`. Index 0 is `""` and unreachable (the branch requires n >= 10).
const TENS: = ;
const HUNDRED: &str = "hundre";
const THOUSAND: &str = "tusen";
const MILLION: &str = "million";
/// What `self.ones[0] if self.ones[0] else "zero"` actually evaluates to.
const ZERO_WORD: &str = "zero";
/// The ceiling of the worded range; at or above it Python returns `str(number)`.
const FALLBACK_AT: u64 = 1_000_000_000;
/// Python's `_int_to_word`.
///
/// Splits the bounded case out into [`int_to_word_small`]: below `FALLBACK_AT`
/// the value provably fits in a u64, and at or above it Python does not word
/// the number at all — it just prints the digits, which `BigInt::to_string`
/// reproduces for any magnitude. So no fixed-width cast is ever applied to a
/// value that could exceed it: `to_u64()` returning `None` (a value past
/// u64::MAX) lands in the same digits branch as 10^9 does.
/// The worded range: `0 < n < 10^9`.
/// The shared `head + " " + word [+ " " + rest]` tail of the three scale
/// branches. Python guards the remainder with `if remainder:`, so a zero
/// remainder appends nothing and never recurses into the "zero" case.
/// `CURRENCY_FORMS`, in Python's insertion order — the order is load-bearing,
/// see [`FALLBACK_INDEX`].
///
/// `Num2Word_NN` declares this dict itself and inherits from `Num2Word_Base`,
/// **not** `Num2Word_EUR`, so the shared-class-dict mutation that
/// `Num2Word_EN.__init__` performs on `Num2Word_EUR.CURRENCY_FORMS` never
/// reaches it. Confirmed against the live interpreter: the runtime table is
/// exactly these three entries, and `CURRENCY_ADJECTIVES` / `CURRENCY_PRECISION`
/// are both `{}` — hence no `currency_adjective`/`currency_precision` override
/// below (precision stays 100 for *every* code, including JPY and KWD).
///
/// Note NOK's subunit is `("øre", "øre")`: identical singular and plural, which
/// is correct Nynorsk and makes the `right != 1` test invisible for that entry.
const FORMS: = ;
/// `list(self.CURRENCY_FORMS.values())[0]` — the `.get()` default in
/// `to_currency` (bug 7). Python dicts iterate in insertion order, and NOK is
/// declared first, so this is always the NOK entry.
const FALLBACK_INDEX: usize = 0;
/// NN's own `to_currency` signature default (`separator=" "`).
const DEFAULT_SEPARATOR: &str = " ";
/// `Num2Word_Base.to_currency`'s signature default. The FFI boundary cannot
/// express per-language default arguments, so the Python shim and the diff
/// harness both pass this literal to mean "caller did not supply one"; see the
/// `separator` handling in [`LangNn::to_currency`].
const BASE_DEFAULT_SEPARATOR: &str = ",";
/// Python's `int(s)` failing on a non-decimal literal.
/// Reconstruct `str(number)` for a `FloatValue::Float`, sign included.
///
/// CPython reprs a float in fixed notation only while its decimal exponent is
/// in `[-4, 16)`; outside that it switches to exponent form (`repr(1e16) ==
/// '1e+16'`, `repr(1e-05) == '1e-05'`), which NN's `int()` then chokes on
/// (bug 9). Inside the fixed window, `format!("{:.p}")` with the repr-derived
/// `precision` reproduces `str()` byte for byte — correctly rounding the
/// binary value to the repr's own digit count re-derives exactly those digits
/// (shortest-round-trip is unique at its own length), and it restores the
/// trailing ".0" that Rust's plain `{}` drops from whole floats. The sign
/// comes from the sign *bit*, so `-0.0` prints "-0.0" and earns the negword.
///
/// In the exponent windows the shortest digits are recovered from Rust's `{}`
/// (also shortest-round-trip, hence the same digits as Python's repr) and
/// re-assembled Python-style: one leading digit, "." only if more digits
/// follow, and a signed, two-digit-minimum exponent (`1e+16`, `1.5e-05`).
/// Every f64 at or above 1e16 is whole, so the high side is all integer
/// digits. Non-finite values print as Python does ('inf'/'nan' — note
/// CPython drops NaN's sign bit); they are binding-unreachable but kept so
/// the surface matches `str(float)` everywhere. (Same reconstruction as the
/// XH port's, which the corpus pins on the identical code shape.)
/// Python's `Num2Word_NN.to_cardinal` applied to `str(number)` — the shared
/// body behind the integer, float and Decimal entries:
///
/// ```text
/// n = str(number).strip()
/// if n.startswith("-"): n = n[1:]; ret = self.negword
/// else: ret = ""
/// if "." in n:
/// left, right = n.split(".", 1)
/// ret += self._int_to_word(int(left)) + " " + self.pointword + " "
/// for digit in right:
/// ret += self._int_to_word(int(digit)) + " "
/// return ret.strip()
/// else:
/// return (ret + self._int_to_word(int(n))).strip()
/// ```
///
/// `int(...)` on a non-numeric token raises ValueError, and exponent-notation
/// `str()`s genuinely reach it (bug 9): `'1e+16'`/`'1E+2'` have no "." so the
/// whole token hits the integer parse, while `'1.5e-05'` splits and then dies
/// in the digit loop on the exponent marker — `int('e')` (uppercase `int('E')`
/// for a Decimal). The error escapes before the negword is applied, matching
/// Python's evaluation order. Both messages mirror CPython's byte for byte,
/// including which token they quote.
/// `forms[1] if n != 1 else forms[0]` — the inline plural test NN's
/// `to_currency` uses in place of `pluralize` (which it never calls).
///
/// Every NN entry carries exactly two forms, so the `Index` arm is
/// unreachable; it exists so a malformed table cannot panic.