Bug 395 - sin(30) does not display correct value
Summary: sin(30) does not display correct value
Status: RESOLVED FIXED
Alias: None
Product: Group37
Classification: Unclassified
Component: calculation operation (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- minor
Assignee: feiwu25-c
URL:
Depends on:
Blocks:
 
Reported: 2020-11-07 16:03 HKT by PAN Zhenyuan
Modified: 2020-11-16 23:40 HKT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description PAN Zhenyuan 2020-11-07 16:03:46 HKT
I was using the Scientific calculator in normal calculation mode in Windows 10 PC. I found a major bug that if I tell it to calculate sin(30), it will display 0.4999 rather than 0.5000.

To reproduce this bug, just run main.java and type 1, enter, and type sin(30) and then enter.
Comment 1 PAN Zhenyuan 2020-11-07 18:55:35 HKT
Overview:
tan(90) will cause the program to get stuck

Step to reproduce:
1) Start the calculator
2) Enter 2 to enter Normal Calculation
3) Enter 2 to input algo
4) Enter sin(30)
5) Press Enter to continue

Expected Result:
sin(30) = 0.5

Actual Result:
sin(30) = 0.4999999999


Build Date & Hardware:
07-11-2020 on Window 10.0.18363
Comment 2 PAN Zhenyuan 2020-11-07 19:01:19 HKT
(In reply to PAN Zhenyuan from comment #1)
> Overview:
> tan(90) will cause the program to get stuck
> 
> Step to reproduce:
> 1) Start the calculator
> 2) Enter 2 to enter Normal Calculation
> 3) Enter 2 to input algo
> 4) Enter sin(30)
> 5) Press Enter to continue
> 
> Expected Result:
> sin(30) = 0.5
> 
> Actual Result:
> sin(30) = 0.4999999999
> 
> 
> Build Date & Hardware:
> 07-11-2020 on Window 10.0.18363

It's a precision problem of Java floating numbers. I will try to fix it using math libraries.
Comment 3 PAN Zhenyuan 2020-11-16 21:53:36 HKT
Fixed using floating precision tricks.
Comment 4 PAN Zhenyuan 2020-11-16 23:40:15 HKT
Reason:Floating numbers can't be very precise when dealing with very small numbers.
Solution:I multiply the floating number by a large number floor it, and then devide it by the same number.