Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:07:46+00:00 2026-05-11T00:07:46+00:00

Is there a more efficient way to clamp real numbers than using if statements

  • 0

Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I’m not asking for code that can handle both cases; they will be handled in separate functions.

Obviously, I can do something like:

double clampedA; double a = calculate(); clampedA = a > MY_MAX ? MY_MAX : a; clampedA = a < MY_MIN ? MY_MIN : a; 

or

double a = calculate(); double clampedA = a; if(clampedA > MY_MAX)     clampedA = MY_MAX; else if(clampedA < MY_MIN)     clampedA = MY_MIN; 

The fixpoint version would use functions/macros for comparisons.

This is done in a performance-critical part of the code, so I’m looking for an as efficient way to do it as possible (which I suspect would involve bit-manipulation)

EDIT: It has to be standard/portable C, platform-specific functionality is not of any interest here. Also, MY_MIN and MY_MAX are the same type as the value I want clamped (doubles in the examples above).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:07:47+00:00Added an answer on May 11, 2026 at 12:07 am

    For the 16.16 representation, the simple ternary is unlikely to be bettered speed-wise.

    And for doubles, because you need it standard/portable C, bit-fiddling of any kind will end badly.

    Even if a bit-fiddle was possible (which I doubt), you’d be relying on the binary representation of doubles. THIS (and their size) IS IMPLEMENTATION-DEPENDENT.

    Possibly you could ‘guess’ this using sizeof(double) and then comparing the layout of various double values against their common binary representations, but I think you’re on a hiding to nothing.

    The best rule is TELL THE COMPILER WHAT YOU WANT (ie ternary), and let it optimise for you.

    EDIT: Humble pie time. I just tested quinmars idea (below), and it works – if you have IEEE-754 floats. This gave a speedup of about 20% on the code below. IObviously non-portable, but I think there may be a standardised way of asking your compiler if it uses IEEE754 float formats with a #IF…?

      double FMIN = 3.13;   double FMAX = 300.44;    double FVAL[10] = {-100, 0.23, 1.24, 3.00, 3.5, 30.5, 50 ,100.22 ,200.22, 30000};   uint64  Lfmin = *(uint64 *)&FMIN;   uint64  Lfmax = *(uint64 *)&FMAX;      DWORD start = GetTickCount();      for (int j=0; j<10000000; ++j)     {         uint64 * pfvalue = (uint64 *)&FVAL[0];         for (int i=0; i<10; ++i)             *pfvalue++ = (*pfvalue < Lfmin) ? Lfmin : (*pfvalue > Lfmax) ? Lfmax : *pfvalue;     }      volatile DWORD hacktime = GetTickCount() - start;      for (int j=0; j<10000000; ++j)     {         double * pfvalue = &FVAL[0];         for (int i=0; i<10; ++i)             *pfvalue++ = (*pfvalue < FMIN) ? FMIN : (*pfvalue > FMAX) ? FMAX : *pfvalue;     }      volatile DWORD normaltime = GetTickCount() - (start + hacktime); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I have http://www.amazon.com/Discrete-Computational-Structures-Computer-Mathematics/dp/0124208509 which seems to be what they're asking… May 11, 2026 at 3:53 pm
  • added an answer This is a fairly basic implementation of a tree, yes.… May 11, 2026 at 3:53 pm
  • added an answer I haven't done any VBA coding for several years, but… May 11, 2026 at 3:53 pm

Related Questions

Is there a more efficient way to convert an HTMLCollection to an Array, other
Is there a sensible way to group Long UTC dates by Day? I would
I need to repeatedly remove the first line from a huge text file using
I am using this command: cut -d: -f2 To sort and reedit text, Is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.