Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Round-off error when computing value inside range

For finite values v0, v1 and value r in [0, 1] range, will the value v, computed as below, always belong to [v0, v1] range, or can it be (slightly) outside due to round off errors?

double v0;  // Finite
double v1;  // Finite
double r;   // In [0, 1]

double v = v0 * r + v1 * (1.0 - r);

if (v0 <= v1)
    assert(v0 <= v && v <= v1);
else
    assert(v1 <= v && v <= v0);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, it can be. Here's an example:

#include <assert.h>

int main() {

    double v0 = 2.670088631008241e-307;
    double v1 = 2.6700889402193536e-307;
    double  r = 0.9999999999232185;

    double v = v0 * r + v1 * (1.0 - r);

    if (v0 <= v1)
        assert(v0 <= v && v <= v1);
    else
        assert(v1 <= v && v <= v0);

    return 0;
}

This produces:

Assertion failed: (v0 <= v && v <= v1), function main, file b.cpp, line 12.

The value of v computed in this case is:

2.67009e-307
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!