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

fix: The precision loss issue of new BigDecimal(double/float) #91

Merged
merged 2 commits into from
Dec 8, 2024

Conversation

qzmer1104
Copy link

new BigDecimal(double/float) can have precision issues because double is based on the IEEE 754 floating-point standard. It uses binary floating-point representation, and some decimal numbers cannot be represented exactly in binary, leading to precision errors.

quzhimin added 2 commits December 7, 2024 14:45
…t uses binary floating-point representation, and some decimal numbers cannot be represented exactly in binary, leading to precision errors.
…t uses binary floating-point representation, and some decimal numbers cannot be represented exactly in binary, leading to precision errors.
@qzmer1104
Copy link
Author

        BigDecimal v1 = BigDecimal.valueOf(0.1).add(BigDecimal.valueOf(0.2));
        BigDecimal v2 = new BigDecimal(0.1).add(new BigDecimal(0.2));
        System.out.println(v1);
        System.out.println(v2);
        System.out.println(v1.compareTo(v2) == 0);

And you'll get

0.3
0.3000000000000000166533453693773481063544750213623046875
false

@neodix42 neodix42 changed the base branch from main to dev December 8, 2024 17:17
@neodix42 neodix42 changed the base branch from dev to main December 8, 2024 17:18
@neodix42 neodix42 changed the base branch from main to dev December 8, 2024 17:20
@neodix42 neodix42 merged commit 65cae20 into neodix42:dev Dec 8, 2024
1 check failed
@qzmer1104 qzmer1104 deleted the feat-fix-utils branch December 19, 2024 07:20
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

Successfully merging this pull request may close these issues.

2 participants