#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Convert unsigned int to float.


    Hi,
    I am calculating one expression in C language. but i am not getting what I want. I have written the fallowing code
    Code:
    Code = value(0x58, 0xFFFF);                        // read register 
     printf("CODE value is %d\r\n", Code);    
    //I will get code value in between "0 to 65535".
               
    // formula to caluculate analog voltage from digital output Code = 2N  1 [(Ain  GAIN/VREF) + 1] 
               // GAIN/VREF=1/2.5=>0.416    //GAIN=1 and VREF=2.5
               //0.416Ain= ((Code/32768)-1)
               Ain=(((float)((Code/32768)-1))/0.416);   
               printf("value:%f [v]\r\n", Ain);
    I have simplified the formula and implemented in C.
    I will get Ain as negative value like -1.8995. I want to print that negative value. I have declared Code as "Unsigned int" and Ain as "float".
    Can any one tel me how to print that.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Code:
    Ain=(((float)((Code/32768)-1))/0.416);
    The highlighted division happens in integer arithmetic, causing you to lose almost all bits of precision. This may work better for you:
    Code:
    Ain=(((((float)Code)/32768)-1)/0.416);
    Here the division is float/int, so the int will be promoted to float before the division. Another way to do this is:
    Code:
    Ain=(((Code/32768.0)-1)/0.416);
    This is int/float, which causes Code to be promoted implicitly to float.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0
    Originally Posted by Lux Perpetua
    Code:
    Ain=(((float)((Code/32768)-1))/0.416);
    The highlighted division happens in integer arithmetic, causing you to lose almost all bits of precision. This may work better for you:
    Code:
    Ain=(((((float)Code)/32768)-1)/0.416);
    Here the division is float/int, so the int will be promoted to float before the division. Another way to do this is:
    Code:
    Ain=(((Code/32768.0)-1)/0.416);
    This is int/float, which causes Code to be promoted implicitly to float.
    Yes I have tried as your suggestion
    Code:
    Ain=(((((float)Code)/32768)-1)/0.416);
     printf("value:%.2f [v]\r\n", Ain);
    as well as this
    Code:
    Ain=(((Code/32768.0)-1)/0.416);
     printf("value:%.2f [v]\r\n", Ain);
    but still I am not able to print that value. Just printing "2f".
    I will get Ain as negative value.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Originally Posted by pavanibiji
    but still I am not able to print that value. Just printing "2f"..
    What target are you running on and what compiler?

    Many C formatted I/O libraries intended for low-end microcontrollers and embedded systems do not support floating point or the support is optional and off by default. The use of floating point on a system without floating point hardware requires a relatively large amount of code space and processing time and is often best avoided.

    In this case scale up your units to millivolts or tenths of a millivolt perhaps and perform the calculation is "fixed point arithmetic". Printing the result in volts can be done by dividing by the scale factor to the the whole number part, then taking abs(Ain) % SCALE_FACTOR) to get the fractional part then print it after the decimal-point with zero-padding to the required number of digits.
    Last edited by clifford; October 19th, 2012 at 12:52 PM.

IMN logo majestic logo threadwatch logo seochat tools logo