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

  • SEARCH
  • Home
  • 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 6184457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:32:21+00:00 2026-05-24T01:32:21+00:00

While writing several math utilities I bumped into need to implement generic utility that

  • 0

While writing several math utilities I bumped into need to implement generic utility that can perform comparisons between any two fundamental arithmetic types. As I began coding, it became clear that this operation is not as straightforward as it seems, since I need correct handling of corner cases, especially when the types have different precision, i.e. rounding strategy during conversion between types becomes important. Consider:

float a1 = 4.8f;
int a2 = 4;
assert(a2 != (int) a1); //fails erroneously since we truncated a1

float b1 = 40000000.0f; //can represent only 40000000 and 40000004 accurately
long b2 = 40000002;
assert(b1 != (float) b2); //fails erroneously since we now truncated b2

The above can be implemented using c++0x type traits to automatically select the appropriate algorithm according to the template arguments supplied to the comparison function. However, this is quite complex and there’s quite a lot of places where bugs can creep, so I don’t think inventing everything myself is worthwhile. Does anyone know a library that implements the above correctly?

  • 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. Editorial Team
    Editorial Team
    2026-05-24T01:32:22+00:00Added an answer on May 24, 2026 at 1:32 am

    You may want to look into GNU’s MP Bignum library at http://gmplib.org/. From their page:

    GMP is a free library for arbitrary precision arithmetic, operating on
    signed integers, rational numbers, and floating point numbers. There
    is no practical limit to the precision except the ones implied by the
    available memory in the machine GMP runs on. GMP has a rich set of
    functions, and the functions have a regular interface.

    GMP is carefully designed to be as fast as possible, both for small
    operands and for huge operands. The speed is achieved by using
    fullwords as the basic arithmetic type, by using fast algorithms, with
    highly optimised assembly code for the most common inner loops for a
    lot of CPUs, and by a general emphasis on speed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt() We
I am writing some PHP. I have several classes that do not declare any
While writing a file using ofstream, how do I know when the file's size
while writing a custom attribute in C# i was wondering if there are any
I got this doubt while writing some code. Is 'bool' a basic datatype defined
I'm engaged in writing a product using LinqToSql for data-access. While we're rapidly developing,
I do write unit tests while writing APIs and core functionalities. But I want
EDIT I just realized that I must have had a massive brain fart while
I have several simple Perl programs writing to the standard output, but this has

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.