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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:50:52+00:00 2026-06-05T06:50:52+00:00

I went upon a very strange behavior (to me) : int generate_scenario_one_pass(FILE *out, double

  • 0

I went upon a very strange behavior (to me) :

    int generate_scenario_one_pass(FILE *out, double freq_mhz) {
        unsigned int d_freq, d_freq_test;
        d_freq              = (int)(freq_mhz * 20);
        d_freq_test         = (int)(float)(freq_mhz * 20);
        printf("when freq_mhz = %.1f, d_freq = 0x%04X, d_freq_test = 0x%04X\n", freq_mhz, d_freq, d_freq_test);
    }

The whole code is not here, but it’s not relevant. This function is called several times with increasing values, starting from 2110.0 with an increment of 0.1.

when freq_mhz = 2110.0, d_freq = 0xA4D8, d_freq_test = 0xA4D8
when freq_mhz = 2110.1, d_freq = 0xA4DA, d_freq_test = 0xA4DA
when freq_mhz = 2110.2, d_freq = 0xA4DC, d_freq_test = 0xA4DC
when freq_mhz = 2110.3, d_freq = 0xA4DD, d_freq_test = 0xA4DE

At the last iteration, d_freq is wrong! But d_freq_test has the correct value. So my issue was solved by casting from double to float, then from float to int. I wanted to know why.

This was compiled using MSVC++ 6.0 on a x86 CPU.

  • 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-05T06:50:53+00:00Added an answer on June 5, 2026 at 6:50 am

    There are many numbers that cannot be represented exactly as a floating-point number – and 0.1 is among them (it will be rounded to the closest number that can be represented – something along the lines of 0.0999999999999998). When using double, 2110.3 happens to be represented by a number that is slightly smaller than 2110.3, thus giving the “wrong” result when you multiply by 20 and cast to int (which will round down), while 2110.3 as a float will be represented by a number that is slightly bigger than 2110.3, thus giving the expected outcome.

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

Sidebar

Related Questions

We stumbled upon a rather strange problem IMO. Our clients have been complaining about
I right clicked my project, went to properties, libraries and added miglayout-4.0-swing.jar, but upon
I went through dozens of answers to figure out the trick to posting data
I have an app that checks for a file upon load from the appDelegate's
I went through this whole procedure http://www.hanselman.com/blog/WorkingWithSSLAtDevelopmentTimeIsEasierWithIISExpress.aspx a couple weeks ago and got https
I went to Twitter's resource page here (https://twitter.com/about/resources/tweetbutton) and got the following code: <a
I went as far as searching C sources, but I can't find this function,
I went to connect to my website via firefox earlier this afternoon and I
I went to download SharpZipLib assemblies but it looks like its not on SourceForge.
I went through a document at Adobe Livedocs that describes working with pdf: http://livedocs.adobe.com/flex/3/html/help.html?content=PDF_1.html

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.