-
-
Notifications
You must be signed in to change notification settings - Fork 6
nanvm: format bigint string coercion as decimal #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fb01c96
d93c80c
da2e908
9757459
61bddaa
b189559
7e7fe29
ac3719f
32da59e
0d9e478
3e758b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use crate::{ | ||
| common::sized_index::SizedIndex, | ||
| sign::Sign, | ||
| vm::{BigInt, IContainer, IVm}, | ||
| }; | ||
| use core::fmt::{Display, Formatter, Result, Write}; | ||
|
|
||
| const DECIMAL_BASE: u64 = 10_000_000_000_000_000_000; | ||
|
|
||
| impl<A: IVm> Display for BigInt<A> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> Result { | ||
| if self.is_zero() { | ||
| return f.write_char('0'); | ||
| } | ||
| if *self.0.header() == Sign::Negative { | ||
| f.write_char('-')?; | ||
| } | ||
|
|
||
| let items = self.0.items(); | ||
| let mut words: Vec<u64> = (0..items.length()).map(|i| items[i]).collect(); | ||
| let mut groups = Vec::new(); | ||
| while !words.is_empty() { | ||
| let mut remainder = 0u128; | ||
| for word in words.iter_mut().rev() { | ||
| let dividend = (remainder << 64) | *word as u128; | ||
| *word = (dividend / DECIMAL_BASE as u128) as u64; | ||
| remainder = dividend % DECIMAL_BASE as u128; | ||
| } | ||
| groups.push(remainder as u64); | ||
| while words.last() == Some(&0) { | ||
| words.pop(); | ||
| } | ||
| } | ||
|
|
||
| write!(f, "{}", groups.pop().unwrap())?; | ||
| for group in groups.iter().rev() { | ||
| write!(f, "{group:019}")?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ mod add; | |
| mod cmp; | ||
| mod debug; | ||
| mod default; | ||
| mod display; | ||
| mod from; | ||
| mod index; | ||
| mod mul; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,8 +61,7 @@ impl<A: IVm> Dispatch<A> for StringCoercion { | |
| } | ||
|
|
||
| fn bigint(self, v: BigInt<A>) -> Self::Result { | ||
| // TODO: we should use different algorithm for large numbers. | ||
| to_result(&format!("{v:?}")) | ||
| to_result(&v.to_string()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This changes the user-visible behavior of bigint string coercion, but the commit adds no AGENTS.md reference: AGENTS.md:L89-L93 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| fn object(self, v: Object<A>) -> Self::Result { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With bigint coercion now enabled,
fjs/nanvm/README.mdlines 84–91 still claims that NaNVM returns hexadecimal text and that these cases carry arustexclusion; it also links to the todo file deleted by this commit. A repo-wide search finds no replacement target, so readers now see both incorrect behavior documentation and a broken link; remove or update that section in this change.Useful? React with 👍 / 👎.