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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:06:39+00:00 2026-05-18T05:06:39+00:00

I am greatly liking WxWidgets, and started with C++ programming in it. My sample

  • 0

I am greatly liking WxWidgets, and started with C++ programming in it. My sample program converts Celsius into Fahrenheit from a text form. Here is my basic code:

//get "100" from textbox
wxString szCelsius = TextCtrl1->GetValue();
long lCelsius;

//attempt to cast into long
szCelsius.ToLong(&lCelsius, 10);

//formula that works in normal cases to get fahrenheit
long lFahrenheit = ((9.f/5.f) * lCelsius + 32);

//SOMEHOW this works:
//long lFahrenheit = ((9.f/5.f) * 100 + 32);

//display debug info, note it displays lCelsius as 100
wxString debuginfo;
debuginfo << _T("deg C: ")  << lCelsius << _T("\n");
//displays incorrectly as 211
debuginfo << _T("deg F: ") << lFahrenheit << _T("\n");
//this displays 100
std::cout << lCelsius;
//this fails though
assert(lCelsius == 100);

Now with the debug info, lCelcius is 100 like expected but it returns fahrenheit as 211 instead of 212! The odd thing is that formula works fine in pure C, and when I replace lCelsius with 100, it works fine, even though my debug info clearly says it is 100.

Do you see any obvious problem or am I just not able to do such a simple thing? I am not sure quite what Wx is doing to make it one less than it should.

EDIT: including assert.h and running lCelsius == 100 fails in debugger, but std::cout lCelsius returns 100. There must be something up with Wx that is mangling the result but still is “100”..

  • 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-18T05:06:40+00:00Added an answer on May 18, 2026 at 5:06 am

    The value 1.8 (which is 9/5) cannot be exactly represented as a binary floating point number – in binary, it is an recurring series of digits (1.1100110011001100110011001100…) – similar to the way 1/3 is recurring in decimal.

    The closest representation as a single-precision floating point value is just under 1.8 – it’s approximately 1.7999999523). When this number is multiplied by 100, it results in a value just under 180; and when 32 is then added, it results in a number just under 212.

    Converting a floating point number to an integer truncates the decimal portion, so 211.999… becomes 211.

    The reason it doesn’t happen if you use a literal 100 in the source code, instead of a runtime-supplied value, is because the compiler simplified the expression (9.f/5.f) * 100 at compile time down to a plain 180.

    If your compiler supports the C99 roundf() function (declared in math.h), you can use that to round to the nearest integer:

    long lFahrenheit = roundf((9.f/5.f) * lCelsius + 32);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think it would greatly simplify function overloading if I could just write the
First, a little background (greatly simplified): I have two classes, one called Entity and
Really simple perl question, but confusing me greatly. foreach $val (@{$obj->something()}) { # this
Can't find it in the Internet. If someone helps I'll appreciate it greatly!
My web-site has AJAX-powered search, that uses deep-linking. When user follows a link …
I a bit new to sqllite lite and linux commands so any help would
I am working on a small project, and am having two tiny problems with
I am attempting to delay load wintrust.dll and crypt32.dll in my application (these are
Ok, so I'm trying to figure out why junit is failing during my ANT

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.