Aim
To implement a four-function calculator with Memory Store (MS) and Memory Recall (MR) operations, and to observe IEEE 754 double arithmetic and value-type copy semantics.
Theory
double is a 64-bit IEEE 754 binary floating-point value type: 1 sign bit, 11 exponent bits and 52 mantissa bits, giving roughly 15–17 significant decimal digits. Unlike integer arithmetic, floating-point division never throws: a / 0.0 yields Infinity and 0.0 / 0.0 yields NaN — a crucial contrast with int division, where dividing by zero raises DivideByZeroException at run time. The "memory" of a physical calculator maps to a variable whose lifetime outlives a single operation: the statement memory = add; COPIES the 64-bit value (value semantics — the two variables are thereafter fully independent; changing add later would not affect memory). This is exactly how the CLR treats all primitive numeric types: assignment copies bits, it never aliases storage. One more professional point: for money, decimal (a 128-bit base-10 type) is the correct choice, because binary floating point cannot represent 0.1 exactly — the classic rounding trap asked in every C# viva.
Requirements
- .NET SDK 8.0+; commands
dotnet new consoleanddotnet run; any editor or the RUN CODE button.
Procedure
- Scaffold with
dotnet new console -n MemoryCalc, thencd MemoryCalc. - Paste the program into
Program.cs. - Run
dotnet runand compare each printed operation with a hand calculation. - Experiment: change
bto0and observeInfinityfor the division; then retype both operands asintand observe the thrownDivideByZeroExceptioninstead.
Explanation of the Code
double a = 20, b = 5;declares the two operands;double memory = 0;is the calculator's M register, zero-initialised like a cleared memory.double add = a + b; memory = add;— the sum is computed once, then value-copied intomemory(Memory Store).addandmemoryare now two independent 64-bit slots in the stack frame.- The four
Console.WriteLine($"...")lines print Add/Sub/Mul/Div using inline interpolation expressions such as{a - b}— interpolation holes may contain ANY expression, not just variable names; each is evaluated, converted withToString, and spliced. Console.WriteLine($"Memory Recall: {memory}")reads the stored value back (Memory Recall) — untouched by the operations printed after the store, which is the experimental proof that assignment copied rather than aliased.
Expected Output
A = 20, B = 5
Add: 25
Sub: 15
Mul: 100
Div: 4
Memory Recall: 25
🎯 Viva Questions
- Why does
20.0 / 0print Infinity instead of crashing? — IEEE 754 defines ±Infinity and NaN for floating point; only integer division throwsDivideByZeroException. - What are value semantics? — Assignment copies the data itself;
memory = addcreates an independent copy, not a reference. - When should
decimalreplacedouble? — For money and exact decimal fractions;doublecannot represent 0.1 exactly in binary. - How many significant digits does
doublehold? — About 15–17 decimal digits (52-bit mantissa plus the implicit leading bit). - Can an interpolation hole contain an expression? — Yes, e.g.
{a * b}; it is evaluated and formatted in place. - Where do these local
doublevariables live at run time? — In the method's stack frame (or in CPU registers after JIT optimisation), not on the GC heap.