2023-04-20 09:37:15 -05:00
|
|
|
//@run-rustfix
|
2021-11-20 08:04:08 -06:00
|
|
|
#![feature(const_fn_floating_point_arithmetic)]
|
2020-02-29 15:46:59 -06:00
|
|
|
#![warn(clippy::suboptimal_flops)]
|
|
|
|
|
2021-11-20 08:04:08 -06:00
|
|
|
/// Allow suboptimal ops in constant context
|
|
|
|
pub const fn in_const_context(num: f64) -> f64 {
|
|
|
|
if num >= 0.0 { num } else { -num }
|
|
|
|
}
|
|
|
|
|
2020-02-28 14:40:13 -06:00
|
|
|
struct A {
|
|
|
|
a: f64,
|
2020-02-29 20:51:39 -06:00
|
|
|
b: f64,
|
2020-02-28 14:40:13 -06:00
|
|
|
}
|
2020-02-28 00:31:41 -06:00
|
|
|
|
|
|
|
fn fake_abs1(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if num >= 0.0 { num } else { -num }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fake_abs2(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if 0.0 < num { num } else { -num }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 14:40:13 -06:00
|
|
|
fn fake_abs3(a: A) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if a.a > 0.0 { a.a } else { -a.a }
|
2020-02-28 14:40:13 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 15:46:59 -06:00
|
|
|
fn fake_abs4(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if 0.0 >= num { -num } else { num }
|
2020-02-29 15:46:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fake_abs5(a: A) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if a.a < 0.0 { -a.a } else { a.a }
|
2020-02-29 15:46:59 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 00:31:41 -06:00
|
|
|
fn fake_nabs1(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if num < 0.0 { num } else { -num }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fake_nabs2(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if 0.0 >= num { num } else { -num }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 14:40:13 -06:00
|
|
|
fn fake_nabs3(a: A) -> A {
|
2020-02-29 20:51:39 -06:00
|
|
|
A {
|
|
|
|
a: if a.a >= 0.0 { -a.a } else { a.a },
|
|
|
|
b: a.b,
|
|
|
|
}
|
2020-02-28 14:40:13 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 00:31:41 -06:00
|
|
|
fn not_fake_abs1(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if num > 0.0 { num } else { -num - 1f64 }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn not_fake_abs2(num: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if num > 0.0 { num + 1.0 } else { -(num + 1.0) }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn not_fake_abs3(num1: f64, num2: f64) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if num1 > 0.0 { num2 } else { -num2 }
|
2020-02-28 00:31:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-28 14:40:13 -06:00
|
|
|
fn not_fake_abs4(a: A) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if a.a > 0.0 { a.b } else { -a.b }
|
2020-02-28 14:40:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn not_fake_abs5(a: A) -> f64 {
|
2021-03-01 11:53:33 -06:00
|
|
|
if a.a > 0.0 { a.a } else { -a.b }
|
2020-02-28 14:40:13 -06:00
|
|
|
}
|
|
|
|
|
2020-03-03 17:52:53 -06:00
|
|
|
fn main() {
|
|
|
|
fake_abs1(5.0);
|
|
|
|
fake_abs2(5.0);
|
2020-03-03 20:06:59 -06:00
|
|
|
fake_abs3(A { a: 5.0, b: 5.0 });
|
2020-03-03 17:52:53 -06:00
|
|
|
fake_abs4(5.0);
|
2020-03-03 20:06:59 -06:00
|
|
|
fake_abs5(A { a: 5.0, b: 5.0 });
|
2020-03-03 17:52:53 -06:00
|
|
|
fake_nabs1(5.0);
|
|
|
|
fake_nabs2(5.0);
|
2020-03-03 20:06:59 -06:00
|
|
|
fake_nabs3(A { a: 5.0, b: 5.0 });
|
2020-03-03 17:52:53 -06:00
|
|
|
not_fake_abs1(5.0);
|
|
|
|
not_fake_abs2(5.0);
|
|
|
|
not_fake_abs3(5.0, 5.0);
|
2020-03-03 20:06:59 -06:00
|
|
|
not_fake_abs4(A { a: 5.0, b: 5.0 });
|
|
|
|
not_fake_abs5(A { a: 5.0, b: 5.0 });
|
2020-03-03 17:52:53 -06:00
|
|
|
}
|