Floating-Points

Floating-Point Numbers

Floating Point Numbers: Real numbers.

Floating-Point Notation:

\text{Floating-Point Notation: } \text{Mantissa} \text{Base}^\text{Exponent}

Example:

NumberScientific NotationFloating-Point Notation
1,245,000,000,0001.24510^{12}0.1245 10^{13}
0.00000012451.245 10^{-7}0.1245 10^{-6}

Representations

Binary Fixed-Point:

We can place an imaginary binary point in a binary pattern to define an integer part and fraction part.

Example: Binary pattern with two fractional bits \to decimal

00101011_2 = 001010.11_2 = 10.75_{10}

Note: For more precision you need to use more floating-point bits (you can shift with sra) .

Floating-Point IEEE-754:

MIPS Floating Point Coprocessor 1

Instructions are split between single and double precision.

    .data
radius: .float  10.0
area:   .float  0.0
    .text
main:
    l.s $f4, radius
    mul.s   $f5, $f4, $f4   # f4 = radius ^ 2
    li.s    $f6, 3.141592653589793  # f6 = pi
    mul.s   $f12, $f5, $f6  # area = pi * radius^2
    # Save result into area
    s.s $f12, area
    # Print area
    li  $v0, 2
    syscall
    # Exit
    li  $v0, 10
    syscall

Tangent: Swapping Values in Two Registers with Xor

On x86, we have 4 general-purpose registers, A, B, C, and D.

Q: How do we swap A and B without using memory or using another GPR?

A: Use the exclusive or operator

A <- A ^ B
B <- A ^ B
A <- A ^ B
xor   $t0, $t0, $t1
xor   $t1, $t0, $t1
xor   $t0, $t0, $t1