Number is always represented with the closest double?
A real number is usually between two double representation.
Always the closest representation is used or not necessary?

Dani AI

Generated

Short answer for : on IEEE-754 systems most basic arithmetic is rounded according to the active rounding mode (the common default is round‑to‑nearest, tie‑to‑even). That is a hardware/ABI behaviour, not something the C++ language strictly mandates, and it can be masked by platform quirks (for example extra precision kept in CPU registers on x86).

Clarification of a point from : floating types store a binary significand and an exponent, not decimal digits, so examples like "1.236 -> 1.23" are misleading. Converting a decimal literal to binary64 is normally done by a rounding algorithm that picks the nearest representable binary value (not simple truncation). To inspect the two neighbouring doubles and see which one an operation produced, use std::nextafter (or print the value in hex float). Example:

#include <iostream>
#include <cmath>

int main() {
    double x = 0.1;
    std::cout << std::hexfloat << x << '\n';
    std::cout << std::hexfloat << std::nextafter(x, 2.0) << '\n';
}

Math library functions are a separate issue: some (sqrt, add, multiply) are often correctly rounded on common hardware, but many transcendental functions (sin/cos/exp) are only guaranteed to be accurate within a few ulps by typical libm implementations. Libraries that aim for guaranteed correct rounding include CRlibm and arbitrary‑precision toolkits like MPFR; consult those if exact rounding of sin/cos is required.

Practical notes tied to s point: to get predictable, platform‑portable rounding behavior, ensure an IEEE‑754 ABI is used, explicitly set/check the rounding mode with <cfenv> (std::fesetround / std::fegetround), and be aware of extended x87 precision or compiler FP optimizations. Further reading: Goldberg’s classic paper on floating point, the <cfenv> reference, and libraries such as CRlibm/MPFR for correctly rounded math.

References: Goldberg paper, cfenv reference, , MPFR.

Recommended Answers

All 4 Replies

Not sure what you are asking. Maybe you should post an example of what you want.

My question is:
Every built in operators (+-*/) and math functions (like: sin, cos, sqrt, ...)
acts on floating point numbers a way like the result is rounded to the nearest representable floating point number?

C++ does not require that, but the IEEE floating-point standard does. So if you're using C++ on an IEEE-conforming machine, it will behave as you want; otherwise you get what you get.

The numbers are not rounded -- excess digits that won't fit in the size of the float or double are just dropped (truncated). Thus if the actual value is 1.236 but only three digits fit in the variable than the variable's value will be 1.23.

Here is a more thorough and accurate description of the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.