microsoft / calculator

Windows Calculator: A simple yet powerful calculator that ships with Windows
MIT License
29.69k stars 5.38k forks source link

Why is |x|/|x-1/x| so slow compared to |x/(x-1/x)|? #1790

Open AwesomeQuest opened 2 years ago

AwesomeQuest commented 2 years ago

Describe the bug

When using the graphing mode, |x|/|x-1/x| is noticeably slower (sub 1FPS on my machine) than |x/(x-1/x)|.

Steps To Reproduce

  1. Go to graphing mode
  2. input |x|/|x-1/x| into function 1
  3. input |x/(x-1/x)| into function 2
  4. compare performance.

Expected behavior

Since the two are largely equivalent one would expect them to preform similarly. Logically it would only be adding one extra operation to each calculation, which I wouldn't expect to cripple performance like it does.

Device and Application Information

Requested Assignment

I'm just reporting this problem. I don't want to fix it.

MicrosoftIssueBot commented 2 years ago

This is your friendly Microsoft Issue Bot. I've seen this issue come in and have gone to tell a human about it.

Liu-jiahui0303 commented 1 year ago

I found that the two expressions given actually represent the same function. In algebra, we can observe that:

The first function: |x|/|x - 1/x| = |x^2|/|x^2 - 1|

The second function: |(x/(x - 1))/x| = |x|/|x - 1| = |x^2|/|x^2 - 1|

This means that their plots and properties should be the same. Now, let's focus on why there is a difference in plotting speed for these two equivalent expressions in the Windows 10 built-in calculator.

The reason could be related to algorithm optimization and numerical computation. During the plotting process, the calculator calculates the function value for each x-value within the defined interval and generates the plot based on these points. Different expressions might cause the calculator to calculate the numerator and denominator separately, or apply some other optimization strategies, such as simplifying the expression. These calculation strategies might affect the drawing speed.

For example, in the first expression, the calculator might need to calculate |x| first, then calculate |x - 1/x|, and finally divide them. In the case of the second expression, the calculator might directly compute the simplified form (|x^2|/|x^2 - 1|). This may result in different computation processes and different numbers of calculations, thereby affecting the plotting speed.

In summary, this speed difference might be due to the calculator's plotting algorithm handling the two expressions differently. Although the two functions are actually equivalent, they may be subject to different evaluation strategies when plotted by the calculator.