Percentage

The WebAssembly module exports a percentage function that takes a value and a percent and returns the value multiplied by the percent divided by 100. It is a plain function rather than a method, so it does not touch the calculator's stored value, memory register, or history. It has no button on the keypad; it is available to any JavaScript that imports the module directly.

What it returns

The function is deliberately the simplest possible definition of a percentage, with no rounding and no special cases. Both arguments are 64-bit floats and so is the result.

Each of those cases is asserted by a unit test in the crate, so the behaviour above is checked rather than described.

Why there is no percent key

The keypad has 24 buttons and none of them is a percent key. The function is imported by the page's JavaScript but never called from it. If you want percentages in the on-screen calculator today, compute them with multiplication and division: 15 percent of 240 is 240 × 15 ÷ 100.

Computing a percentage on the keypad

Because there is no percent key, percentages are done with the keys that exist. To find 15 percent of 240, enter 240, press ×, enter 15, press =, then press ÷, enter 100 and press = again. The result is 36. To add a percentage rather than take one, compute the percentage first, store it with MS, recall the original value, and add the stored figure back with the arithmetic keys.

Both routes go through the same four arithmetic operators as any other calculation, so each step lands in the history panel and each one is subject to the same 64-bit floating point rules described on the operations page. A percentage of a percentage compounds the same rounding, so a long chain of percentage steps can drift in the last digit or two.

Questions and answers

Does the Rust WASM Calculator have a percent button?

No. The percentage function is exported by the WebAssembly module and can be imported from JavaScript, but the on-screen keypad has no percent key. You can compute a percentage with multiplication and division instead.

Does the percentage function round its result?

No. It multiplies the value by the percent divided by 100 in 64-bit floating point and returns the result unrounded.