math: Rename ZeroScaleMode

It's no longer only for the zero scale.
This commit is contained in:
Mahmoud Al-Qudsi 2024-06-23 17:25:59 -05:00
parent 480d48351c
commit c0028a0ec9

View File

@ -7,13 +7,13 @@ use crate::tinyexpr::te_interp;
/// The maximum number of points after the decimal that we'll print. /// The maximum number of points after the decimal that we'll print.
const DEFAULT_SCALE: usize = 6; const DEFAULT_SCALE: usize = 6;
const DEFAULT_ZERO_SCALE_MODE: ZeroScaleMode = ZeroScaleMode::Default; const DEFAULT_SCALE_MODE: ScaleMode = ScaleMode::Default;
/// The end of the range such that every integer is representable as a double. /// The end of the range such that every integer is representable as a double.
/// i.e. this is the first value such that x + 1 == x (or == x + 2, depending on rounding mode). /// i.e. this is the first value such that x + 1 == x (or == x + 2, depending on rounding mode).
const MAX_CONTIGUOUS_INTEGER: f64 = (1_u64 << f64::MANTISSA_DIGITS) as f64; const MAX_CONTIGUOUS_INTEGER: f64 = (1_u64 << f64::MANTISSA_DIGITS) as f64;
enum ZeroScaleMode { enum ScaleMode {
Truncate, Truncate,
Round, Round,
Floor, Floor,
@ -25,7 +25,7 @@ struct Options {
print_help: bool, print_help: bool,
scale: usize, scale: usize,
base: usize, base: usize,
zero_scale_mode: ZeroScaleMode, scale_mode: ScaleMode,
} }
fn parse_cmd_opts( fn parse_cmd_opts(
@ -50,7 +50,7 @@ fn parse_cmd_opts(
print_help: false, print_help: false,
scale: DEFAULT_SCALE, scale: DEFAULT_SCALE,
base: 10, base: 10,
zero_scale_mode: DEFAULT_ZERO_SCALE_MODE, scale_mode: DEFAULT_SCALE_MODE,
}; };
let mut have_scale = false; let mut have_scale = false;
@ -81,13 +81,13 @@ fn parse_cmd_opts(
'm' => { 'm' => {
let optarg = w.woptarg.unwrap(); let optarg = w.woptarg.unwrap();
if optarg.eq(utf32str!("truncate")) { if optarg.eq(utf32str!("truncate")) {
opts.zero_scale_mode = ZeroScaleMode::Truncate; opts.scale_mode = ScaleMode::Truncate;
} else if optarg.eq(utf32str!("round")) { } else if optarg.eq(utf32str!("round")) {
opts.zero_scale_mode = ZeroScaleMode::Round; opts.scale_mode = ScaleMode::Round;
} else if optarg.eq(utf32str!("floor")) { } else if optarg.eq(utf32str!("floor")) {
opts.zero_scale_mode = ZeroScaleMode::Floor; opts.scale_mode = ScaleMode::Floor;
} else if optarg.eq(utf32str!("ceiling")) { } else if optarg.eq(utf32str!("ceiling")) {
opts.zero_scale_mode = ZeroScaleMode::Ceiling; opts.scale_mode = ScaleMode::Ceiling;
} else { } else {
streams streams
.err .err
@ -164,12 +164,12 @@ fn format_double(mut v: f64, opts: &Options) -> WString {
v *= pow(10f64, opts.scale); v *= pow(10f64, opts.scale);
v = match opts.zero_scale_mode { v = match opts.scale_mode {
ZeroScaleMode::Truncate => v.trunc(), ScaleMode::Truncate => v.trunc(),
ZeroScaleMode::Round => v.round(), ScaleMode::Round => v.round(),
ZeroScaleMode::Floor => v.floor(), ScaleMode::Floor => v.floor(),
ZeroScaleMode::Ceiling => v.ceil(), ScaleMode::Ceiling => v.ceil(),
ZeroScaleMode::Default => { ScaleMode::Default => {
if opts.scale == 0 { if opts.scale == 0 {
v.trunc() v.trunc()
} else { } else {