Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison of fixed point numbers delivers wrong result in some situations #9

Open
sea212 opened this issue Jul 9, 2021 · 0 comments

Comments

@sea212
Copy link

sea212 commented Jul 9, 2021

Comparing some fixed point numbers results always in "false". The original fixed library does not have this problem. Further information and code example below.

System
OS: # 86~20.04.1-Ubuntu - Kernel 5.4.0-77-generic - x86_64 GNU/Linux
CPU: Ryzen 5 3600
RAM: 32 GB - 2x D416GB 3600-17 Viper Steel - 0B swap

Minimal example
tested with rustc nightly-2021-03-10-x86_64-unknown-linux-gnu and stable-x86_64-unknown-linux-gnu.

using substrate-fixed @ b33d186:

[dependencies.fixed]
default-features = false
git = "https://github.com/encointer/substrate-fixed"
package = "substrate-fixed"
rev = "b33d186888c60f38adafcfc0ec3a21aab263aef1"
use fixed::{FixedI128, FixedU128, types::extra::U64};
use fixed::traits::ToFixed;

fn main() {
	let max_u128u64 = <FixedU128<U64>>::max_value();
	let max_i128u64 = <FixedI128<U64>>::max_value();

	println!("{} < {}: {}", max_i128u64, max_u128u64, max_i128u64 < max_u128u64);
	println!("{} == {}: {}", max_i128u64, max_u128u64, max_i128u64 == max_u128u64);
	println!("{} > {}: {}", max_i128u64, max_u128u64, max_i128u64 > max_u128u64);
	println!("");
	let some_i128u64: FixedI128<U64> = 123456790.to_fixed();
	println!("{} < {}: {}", some_i128u64, max_u128u64, some_i128u64 < max_u128u64);
	println!("{} == {}: {}", some_i128u64, max_u128u64, some_i128u64 == max_u128u64);
	println!("{} > {}: {}", some_i128u64, max_u128u64, some_i128u64 > max_u128u64);
}

9223372036854775807.99999999999999999995 < 18446744073709551615.99999999999999999995: false
9223372036854775807.99999999999999999995 == 18446744073709551615.99999999999999999995: false
9223372036854775807.99999999999999999995 > 18446744073709551615.99999999999999999995: false

123456790 < 18446744073709551615.99999999999999999995: false
123456790 == 18446744073709551615.99999999999999999995: false
123456790 > 18446744073709551615.99999999999999999995: false

The same with the original fixed library @ v1.9.0:

use fixed::{FixedI128, FixedU128, types::extra::U64};
use fixed::traits::ToFixed;

fn main() {
	let max_u128u64 = <FixedU128<U64>>::from_bits(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF);
	let max_i128u64 = <FixedI128<U64>>::from_bits(0x7FFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF);

	println!("{} < {}: {}", max_i128u64, max_u128u64, max_i128u64 < max_u128u64);
	println!("{} == {}: {}", max_i128u64, max_u128u64, max_i128u64 == max_u128u64);
	println!("{} > {}: {}", max_i128u64, max_u128u64, max_i128u64 > max_u128u64);
	println!("");
	let some_i128u64: FixedI128<U64> = 123456790.to_fixed();
	println!("{} < {}: {}", some_i128u64, max_u128u64, some_i128u64 < max_u128u64);
	println!("{} == {}: {}", some_i128u64, max_u128u64, some_i128u64 == max_u128u64);
	println!("{} > {}: {}", some_i128u64, max_u128u64, some_i128u64 > max_u128u64);
}

9223372036854775807.99999999999999999995 < 18446744073709551615.99999999999999999995: true
9223372036854775807.99999999999999999995 == 18446744073709551615.99999999999999999995: false
9223372036854775807.99999999999999999995 > 18446744073709551615.99999999999999999995: false

123456790 < 18446744073709551615.99999999999999999995: true
123456790 == 18446744073709551615.99999999999999999995: false
123456790 > 18446744073709551615.99999999999999999995: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant