Skip to content

Commit

Permalink
Print constants in scientific precision (#8506)
Browse files Browse the repository at this point in the history
When the absolute value of a floating point number is smaller than then
default IRPrinter precision (6 decimal points), print the value in
scientific format.

Use cases:

- Normalizing the 2D/3D fft image by the total pixel count.
- Algorithm problem scaling.
- SI unit conversions, i.e. from nanometer to meter.
  • Loading branch information
antonysigma authored Dec 11, 2024
1 parent af2ffe4 commit 65adb9c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/IRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ void IRPrinter::visit(const UIntImm *op) {
}

void IRPrinter::visit(const FloatImm *op) {
const bool use_scientific_format = (op->value != 0.0) && (std::log10(std::abs(op->value)) < -6);
if (use_scientific_format) {
stream << std::scientific;
}

switch (op->type.bits()) {
case 64:
stream << op->value;
Expand All @@ -607,6 +612,10 @@ void IRPrinter::visit(const FloatImm *op) {
default:
internal_error << "Bad bit-width for float: " << op->type << "\n";
}

if (use_scientific_format) {
stream << std::fixed;
}
}

void IRPrinter::visit(const StringImm *op) {
Expand Down

0 comments on commit 65adb9c

Please sign in to comment.