Bug 497850

Summary: Square of -2 produces -4
Product: [Applications] kcalc Reporter: Kirill <kyrylo.shakirov>
Component: generalAssignee: Gabriel Barrantes <gabriel.barrantes.dev>
Status: CONFIRMED ---    
Severity: major CC: gabriel.barrantes.dev, p.r.worrall
Priority: NOR    
Version First Reported In: 24.12.0   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: screenshot of kcalc demonstrating the bug

Description Kirill 2024-12-24 00:29:22 UTC
Created attachment 176857 [details]
screenshot of kcalc demonstrating the bug

SUMMARY
Square of -2 produces -4 when it must be 4

STEPS TO REPRODUCE
1. Put kcalc to engineering mode
2. Press 2
3. Press change sign button (+/-)
4. Press square button (x2)

OBSERVED RESULT
-4

EXPECTED RESULT
4

SOFTWARE/OS VERSIONS
Operating System: EndeavourOS 
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.9.0
Qt Version: 6.8.1
Kernel Version: 6.12.6-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 5800H with Radeon Graphics
Memory: 58.7 ГиБ of RAM
Graphics Processor: AMD Radeon Graphics
Comment 1 Gabriel Barrantes 2024-12-24 00:56:43 UTC
powers takes precedence over + or -, so -2^2 is equal to -4 according to that agreement. You can also enter it on wolframalpha to validate.
Comment 2 Gabriel Barrantes 2024-12-24 01:04:04 UTC
I guess the confusion is due to the old kcalc not having input display, but now the result agrees with the input you are seeing on top. So now to have (-2)^2 you need to actually add the parenthesis.
Comment 3 Kirill 2024-12-24 02:58:38 UTC
+/- button on calculators do not add "-" character to expression, but reverses it sign.

But if we suppose it does - what is not a common convention.
Then, following input sequences must work and produce valid results:

1. "(", "2", "+/-"
Should produce "(-2", but produces "-2"

2. "(", "+/-"
Should produce "(-" but produces "–(()" and "malformed expression" error.
Comment 4 Gabriel Barrantes 2024-12-24 13:58:32 UTC
(In reply to Kirill from comment #3)
> +/- button on calculators do not add "-" character to expression, but
> reverses it sign.
This is what is doing, try a long expression.

> 1. "(", "2", "+/-"
> Should produce "(-2", but produces "-2"
The result is 2, so reversing yields -2

> 2. "(", "+/-"
> Should produce "(-" but produces "–(()" and "malformed expression" error.
This might be a bug, still, kinda expected, because what it does is -(expression).
Comment 5 Paul Worrall 2025-01-29 15:03:08 UTC
I agree with Kirill.  

Both the 'real' calculators I found in my drawer (different brands), and all the ones I can remember using in the past, pressing 2, +/-, x^2 gives 4.
Comment 6 Gabriel Barrantes 2025-01-29 16:54:09 UTC
(In reply to Paul Worrall from comment #5)
> I agree with Kirill.  
> 
> Both the 'real' calculators I found in my drawer (different brands), and all
> the ones I can remember using in the past, pressing 2, +/-, x^2 gives 4.

I rechecked and it makes sense, somebody might fix it soon.