Skip to content

Fix float accuracy issues#12

Merged
chickenlj merged 3 commits intoapache:masterfrom
CrazyHZM:floatBug
Nov 17, 2019
Merged

Fix float accuracy issues#12
chickenlj merged 3 commits intoapache:masterfrom
CrazyHZM:floatBug

Conversation

@CrazyHZM
Copy link
Copy Markdown
Member

dubbo泛化调用,float类型数据结果不正确
apache/dubbo#3861

@codecov-io
Copy link
Copy Markdown

codecov-io commented Apr 12, 2019

Codecov Report

Merging #12 into master will increase coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #12      +/-   ##
============================================
+ Coverage     24.46%   24.63%   +0.16%     
- Complexity      460      465       +5     
============================================
  Files            89       89              
  Lines          6302     6304       +2     
  Branches       1048     1048              
============================================
+ Hits           1542     1553      +11     
+ Misses         4545     4536       -9     
  Partials        215      215
Impacted Files Coverage Δ Complexity Δ
...alibaba/com/caucho/hessian/io/BasicSerializer.java 14.89% <100%> (+1.85%) 6 <0> (+1) ⬆️
...om/alibaba/com/caucho/hessian/io/HessianInput.java 19.55% <0%> (+0.17%) 46% <0%> (+1%) ⬆️
...m/alibaba/com/caucho/hessian/io/Hessian2Input.java 19.93% <0%> (+0.23%) 65% <0%> (+1%) ⬆️
...libaba/com/caucho/hessian/util/IdentityIntMap.java 39.28% <0%> (+7.14%) 9% <0%> (+2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5a74492...a8cb0b2. Read the comment docs.

break;

case FLOAT:
out.writeDouble(Double.parseDouble(((Number) obj).floatValue() + ""));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of + ""?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I have modified it, please review it.

break;

case FLOAT:
out.writeDouble(Double.parseDouble(String.valueOf(((Number) obj).floatValue())));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is out.writeFloat(((Number) obj).floatValue()) not ok?

Copy link
Copy Markdown
Member Author

@CrazyHZM CrazyHZM Apr 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no such method writeFloat()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then how about out.writeDouble(((Number) obj).floatValue()) ? I am concerning the float->String->double conversion, the intermediate String conversion should be avoid.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When single precision is double precision, double precision will complement the single precision. Lead to deviation.
For example:
99.8F -> 99.80000305175781

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I got what you mean. It actually is:

jshell> float f = 99.8f
f ==> 99.8

jshell> double d = f;
d ==> 99.80000305175781

I do not have permission to merge this. I will approve this change.
This repo should be transfer to ASF soon. You should also have permission.

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.

4 participants