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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:50:48+00:00 2026-06-17T12:50:48+00:00

I Have this simple code. union { unsigned int res; char bytes[2]; } ADC;

  • 0

I Have this “simple” code.

union
{
    unsigned int res;
    char         bytes[2];
} ADC;

char ADC_num[5];
float temprature;

void vis_temp (void)        //  Show temp..
{ 
    signed int l, length;
    unsigned int rem;

    GO_nDONE=1;             // initiate conversion on the channel 0   

    while (GO_nDONE) continue;

    ADC.bytes[0]=ADRESL;
    ADC.bytes[1]=ADRESH;

    utoa(ADC_num, ADC.res, 10);

    temprature = (float) ADC.res * 478.1 / 1024;
    temprature = temprature - 50.0;

    l = (signed int) temprature;
    temprature -= (float) l;  
    rem = (unsigned int)(temprature* 1e1);

    sprintf(&ADC_num, "%i.%u", l, rem);

When reading ADC_res (voltage on pin, temperature sensor) that temperature is 0 degree or under then program writes “0.65500” instead of “-3.5” or similar.
I should have declared the right as signed and unsigned int.
Any hints to fix it, or have an other way of converting it.

  • 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-17T12:50:50+00:00Added an answer on June 17, 2026 at 12:50 pm
    temprature = (float) ADC.res * 478.1 / 1024;
    temprature = temprature - 50.0;
    

    Suppose now temprature has a negative value -x.yz.

    l = (signed int) temprature;
    

    Now l = -x, and

    temprature -= (float) l;
    

    temprature = -x.yz - (-x) = -0.yz.

    rem = (unsigned int)(temprature* 1e1);
    

    Multiply with 10, and convert to unsigned int. Usually, that results in undefined behaviour (6.3.1.4 (1)):

    When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.61)

    61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

    But converting the negative value to unsigned int would produce the wrong result anyway, even if the remaindering operation is done, what you want is the absolute value, so you should convert

    rem = (unsigned int)fabsf(temprature * 1e1);
    

    there.

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

Sidebar

Related Questions

I have this simple code: #include <stdio.h> #include <time.h> int main(){ enum Days {
I have this simple code: max = (int) sqrt (number); and in the header
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have this simple code in a WPF application: ThreadStart start = delegate() {
I have this simple code where I am sending http request and reading all
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this simple code in my user_controller.rb file #listing all users def index
i have this simple code to load data from other php page using get
I have this simple code public String toString() { **Iterator it = list.iterator();** String

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.