Skip to content

Commit 65adb9c

Browse files
authored
Print constants in scientific precision (#8506)
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.
1 parent af2ffe4 commit 65adb9c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/IRPrinter.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ void IRPrinter::visit(const UIntImm *op) {
594594
}
595595

596596
void IRPrinter::visit(const FloatImm *op) {
597+
const bool use_scientific_format = (op->value != 0.0) && (std::log10(std::abs(op->value)) < -6);
598+
if (use_scientific_format) {
599+
stream << std::scientific;
600+
}
601+
597602
switch (op->type.bits()) {
598603
case 64:
599604
stream << op->value;
@@ -607,6 +612,10 @@ void IRPrinter::visit(const FloatImm *op) {
607612
default:
608613
internal_error << "Bad bit-width for float: " << op->type << "\n";
609614
}
615+
616+
if (use_scientific_format) {
617+
stream << std::fixed;
618+
}
610619
}
611620

612621
void IRPrinter::visit(const StringImm *op) {

0 commit comments

Comments
 (0)