Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,26 @@ impl PyComplex {
#[pymethod(magic)]
fn repr(&self) -> String {
let Complex64 { re, im } = self.value;
if re == 0.0 {
let re_part = if re.is_nan() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, here's a another idea. We can re-use float formatting to get the job done (and get long float values for the real and imag parts to print out in scientific notation).

Inside float_ops we have to_string that takes an f64 and returns it nicely formatted (it also includes handling of the NaN). The main issue is the sign we add, if its a nan for imag we must always prefix with a "+" (granted that a real part exists).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really great suggestion, sorry I did not check float_ops before 😞
Changing this right now.

"nan".to_string()
} else {
format!("{}", re)
};
let im_part = if im.is_nan() {
if re == 0.0 {
"nanj".to_string()
} else {
"+nanj".to_string()
}
} else if re == 0.0 {
format!("{}j", im)
} else {
format!("({}{:+}j)", re, im)
format!("{:+}j", im)
};
if re == 0.0 {
format!("{}", im_part)
} else {
format!("({}{})", re_part, im_part)
}
}

Expand Down