Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

Can float and double represent 1.2 and 1.5 accurately? I know it can represent 0.5…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    Can float and double represent 1.2 and 1.5 accurately? I know it can represent 0.5 accurately.

  2. Meta Assistant

    A question that gets to the heart of floating-point arithmetic!

    You're absolutely right to be curious about this. The answer is: it depends.

    Let's dive into the world of binary floating-point representation, which is how computers store float and double values.

    The short story:

    • 1.5 can be represented accurately by both float and double.
    • 1.2 cannot be represented accurately by either float or double.

    The long story:

    Computers use binary fractions (base 2) to represent floating-point numbers. A binary fraction is a sum of powers of 2:

    ± 1.b1 b2 b3 ... bn × 2^e

    where b1, b2, ..., bn are binary digits (0 or 1), and e is the exponent (also an integer).

    Now, let's examine the decimal values 0.5, 1.2, and 1.5 and see how they can be represented as binary fractions:

    1. 0.5: Decimal: 0.5 = Binary: 0.1 (exactly!) = 1 × 2^(-1) Easy peasy! 0.5 is a power of 2 (2^-1), so it's represented exactly in binary.
    2. 1.5: Decimal: 1.5 = Binary: 1.1 (exactly!) = 1 × 2^0 + 1 × 2^(-1) Also a sum of powers of 2 (2^0 + 2^-1). Exact representation in binary.
    3. 1.2: Decimal: 1.2 = Binary: 1.001100110011... ( repeating pattern!) ≈ 1 × 2^0 + 0 × 2^(-1) + 0 × 2^(-2) + 1 × 2^(-3) + 1 × 2^(-4) + ... Uh-oh! 1.2 is not a finite sum of powers of 2. The binary representation has an infinite, non-repeating pattern (a dyadic rational). This means 1.2 cannot be represented exactly in binary.

    Here's the kicker: float and double have a finite number of bits to store the binary fraction:

    • float: 32 bits, with 23 bits for the fraction ( mantissa)
    • double: 64 bits, with 52 bits for the fraction (mantissa)

    When you try to store 1.2 in a float or double, the infinite binary pattern is truncated to fit the available bits. This introduces a rounding error.

    For 1.2, the closest approximations are:

    • float: 1.19999992847442626953125 (not exact, but close)
    • double: 1.1999999999999999555910790149937383830547332763671875 (even closer, but still not exact)

    In contrast, 1.5 and 0.5 have exact binary representations, so they're stored precisely in both float and double.

    Code demo (C):

    #include <stdio.h>
    
    int main() {
        float f1 = 1.5f;
        float f2 = 1.2f;
        double d1 = 1.5;
        double d2 = 1.2;
    
        printf("%.20f\n", f1);  // 1.50000000000000000000
        printf("%.20f\n", f2);  // 1.19999992847442626953
        printf("%.40f\n", d1);  // 1.50000000000000000000000000000000000000
        printf("%.40f\n", d2);  // 1.19999999999999995559107901499373838305
    
        return 0;
    }
    

    In summary:

    ValueExact in float & double?
    0.5Yes
    1.5Yes
    1.2No

    When working with floating-point numbers:

    • Be aware of the representation limits.
    • Expect small errors when dealing with decimal values that cannot be exactly represented in binary (like 1.2).
    • Use decimal types (if available in your language) or epsilon comparisons (abs(a - b) < small_value) for robust floating-point arithmetic.

    Now, go forth and wield your newfound floating-point wisdom!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.