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 8006351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:29:12+00:00 2026-06-04T17:29:12+00:00

I would like to encode a pair of ints in a double. For example

  • 0

I would like to encode a pair of ints in a double. For example say i wanted to pass a function:

foo(int a, int b)

but instead I want just one double to represent the two ints (ie) :

foo(double aAndB)

Currently I am doing it by having one int on either side of the decimal place (ie 10 and 15 would become 10.15) and then converting it to a stringstream tokenising and extracting the two numbers.

However, this has an obvious flaw when it comes to numbers like 10 and 10 ie it becomes 10.1.

Is there a way to do this through some tricky mathematical method so that I can pass a function a double that represents 2 ints?

Thanks.

  • 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-06-04T17:29:14+00:00Added an answer on June 4, 2026 at 5:29 pm

    Since (usually) a double has 64 bits in it and each int has 32 bits, you’d think that you could just store the bits into the double directly, e.g.:

    int32_t i1 = rand();
    int32_t i2 = rand();
    int64_t x = (((int64_t)i1)<<32) | ((int64_t)i2);
    double theDouble;
    memcpy(&theDouble, &x, sizeof(theDouble));
    

    … and doing that “almost works”. That is, it works okay for many possible values of i1 and i2 — but not for all of them. In particular, for IEEE754 floating point format, any values where the exponent bits are set to 0x7ff will be treated as indicating “NaN”, and the floating point hardware can (and does) convert different NaN-equivalent bit-patterns back to its preferred NaN bit-pattern when passing a double as an argument, etc.

    Because of this, stuffing two 32-bit integers into a double will appear to work in most cases, but if you test it with all possible input values you’ll find some cases where the values unexpectedly mutated during their stay inside the double, and came out as different values when you decoded them again.

    Of course, you could get around this by being careful only to set the mantissa bits of the double, but that will only give you 26 bits per integer, so you would only be able to store integer values of +/- 33,554,432 or so. Maybe that’s okay, depending on your use case.

    My advice is, find a different way to do whatever you’re trying to do. Storing non-floating-point data in a floating point variable is asking for trouble, especially if you want your code to be at all portable.

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

Sidebar

Related Questions

How to encode URLs containing Unicode? I would like to pass it to a
I would like to encode my URL, but I want to convert spaces to
I would like to encode user input values coming from a web interface in
I would like to encode data into a binary format in a buffer which
I would like to encode some plain text using Ruby and the Crypt library
I would like to use PKCS7 encryption to encode a value together with Java
I would like to encode ITSATEST to it's netbios name value in python; The
I have the following string that I would like to Huffman-encode and store efficiently
What is the advantage of using Base64 encode? I would like to understand it
Im using JDom for creating and retrieving XML messages. I would like to encode

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.