Conversion from float to char array - c

I need to convert my float (sensor) value to a char array for sending my data through GPRS. If I use any library function for converting, it takes a lot of memory, because the controller has less amount of flash. I have tried my best to convert the data without a library function, but finally it needs the math.h file compulsorily. How can I solve this issue without any library function?
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned char str[20];
unsigned char *ftos(float f,int precision)
{
memset(str,0,sizeof(str));
float ff;
ff = f;
int a,b,c,k,l=0,m,i=0;
// check for negetive float
if(f<0.0)
{
str[i++]='-';
f*=-1;
}
a=f; // extracting whole number
f-=a; // extracting decimal part
k = precision;
// number of digits in whole number
while(k>0)
{
l = pow(10,k);
m = a/l;
if(m>0)
{
break;
}
k--;
}
// number of digits in whole number are k+1
/*
extracting most significant digit i.e. right most digit , and concatenating to string
obtained as quotient by dividing number by 10^k where k = (number of digit -1)
*/
for(l=k+1;l>0;l--)
{
b = pow(10,l-1);
c = a/b;
str[i++]=c+48;
a%=b;
}
str[i++] = '.';
/* extracting decimal digits till precision */
for(l=0;l<precision;l++)
{
f*=10.0;
b = f;
str[i++]=b+48;
f-=b;
}
str[i]='\0';
return str;
}
int main()
{
float temp = 35.2;
printf("%s",ftos(temp,2));
}

Don't try to print a floating point value, print a fixed point value instead. For example, this prints value of x with 2 digits after the decimal point:
int main()
{
float x = 35.2;
printf("%d.%02d\n", (int)x, (int)(x * 100) - (int)x * 100);
}
If you need to actually convert the value to a char array, use sprintf instead of printf. Be careful to avoid integer overflows when multiplying, especially if your platform has 16-bit integers: use long values if required.
Overall, there are very few cases when printing floating point numbers is a good idea from a microcontroller C code.

Related

How would I produce an integer from a float in the sense of removing the decimal point, despite floating-point precision errors?

In C, how can I produce, for example 314159 from 3.14159 or 11 from 1.1 floats? I may not use #include at all, and I am not allowed to use library functions. It must be completely cross platform, and fit in a single function.
I tried this:
while (Number-(int)Number) {
Number *= 10;
}
and this:
Number *= 10e6;
and floating-point precision errors get in my way. How can I do this? How can I accurately transform all digits in a float into an integer?
In response to a comment, they are a float argument to a function:
char *FloatToString(char *Dest, float Number, register unsigned char Base) {
if (Base < 2 || Base > 36 || !Dest) {
return (char *)0;
}
char *const RDest = Dest;
if (Number < 0) {
Number = -Number;
*Dest = '-';
Dest++;
}
register unsigned char WholeDigits = 1;
for (register unsigned int T = (int)Number/Base; T; T /= Base) {
WholeDigits++;
}
Dest[WholeDigits] = '.';
// I need to now effectively "delete" the decimal point to further process it. Don't answer how to convert a float to a string, answer the title.
return RDest;
}
The essential problem you have is that floating point numbers can't represent your example numbers, so your input is always going to be slightly different. So if you accurately produce output, it will be different from what you expect as the input numbers are different from what you think they are.
If you don't have to worry about very large numbers, you can do this most easily by converting to a long:
v = v - (long)v; // remove the integer part
int frac = (int)(v * 100000);
will give you the 5 digits after the decimal point. The problem with this is that it give undefined behavior if the initial value is too large to be converted to a long. You might also want to be rounding differently (converting to int truncates towards zero) -- if you want the closest value rather than the leading 5 digits of the fraction, you can use (int)(v * 100000 + (v > 0 ? 0.5 : -0.5))
New version :
#include <stdio.h>
int main()
{
double x;
int i;
char s[10];
x = 9999.12504;
x = (x-(int)x);
sprintf(s,"%0.5g\n",x);
sscanf((s+2),"%d",&i);
printf("%d",i);
return 0;
}
Old version
#include <stdio.h>
int main()
{
float x;
int i;
x = -3.14159;
x = (x-(int)x);
if (x>=0)
i = 100000*x;
else
i = -100000*x;
printf("%d",i);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
int main(void) {
double t = 0.12;
unsigned long x = 0;
t = (t<0)? -t : t; // To handle negative numbers.
for(t = t-(int)t; x < ULONG_MAX/10; t = 10*t-(int)(10*t))
{
x = 10*x+(int)(10*t);
}
printf("%lu\n", x);
return 0;
}
Output:
11999999999999999644
I feel like you should use modulo to get the decimal portion, convert it to a string, count the number of characters, and use that to multiply your remainder before casting it to an int.

How to extract digits after decimal point in float and store in char Array

I refered below question but I need to extract the digits after decimal and store in char *array
1.How to store float in array of float[]
2.Extract digits in float in python
Ex:
float = 1.24456
My length of char *array is 2
Expected Output:
array[0] = 2;
array[1] = 4;
array[2] = 4;
I need to implement this in C dynamically.
You can isolate the fractional part of a float by using the function modff in <math.h>, which returns the fractional part as the function's return value and the whole-number part as an argument by reference:
float f = 3.141592f;
float numpart, fracpart;
fracpart = modff(f, &numpart);
Once you're done that, you can create a string with a conventional string-building function:
char buf[100];
snprintf(buf, 100, "%f", fracpart);
Another option is converting the entire float to a string, and then using strchr(float_str, '.') to isolate the decimal part.
Slightly more mathematical approach, but arguably less flexible would be this:
#include <stdio.h>
#include <math.h>
int main()
{
float num = 1.24456;
int num_noDecimals = (int)num; //truncate decimals in original number
float num_onlyDecimals = num - (float)num_noDecimals; //obtain only the number after decimal
int x = 4; //number of digits past decimal to extract
num_onlyDecimals = num_onlyDecimals*pow(10,x); //move extracting digits left of decimal
int digits_forArray = (int)num_onlyDecimals; //truncate remaining digits right of decimal
char arr[x];
for(int i=x-1; i > -1; i--) { arr[i] = (char)(digits_forArray%10); digits_forArray = digits_forArray/10; }
for(int i=0;i<x;i++) { printf("arr[%d] = %d\n",i,arr[i]); }
}
Hopefully it's commented well enough to understand the steps being taken here.
In my code, I removed the integer part, then I reverse the float part to integer part by multiply each number of the float part by the number 10 then I classify each digit of in the array
My code :
int main()
{
float n = 1.2445601;
int m=(int)n;
float p=n-m;
int x=7,i=0,tab[x];
while(i<x)
{
p=p*10;
i++;
}
i=6;
while((int)p!=0)
{
tab[i--]=(int)p%10;
p=p/10;
}
for(int k=0;k<x;k++)
{
printf("\narray[%d] = %d ;",k,tab[k]);
}
printf("\n");
return 0;
}
If you just want the digits after the decimal point in variable, you could just have number - floor(number)
for example, if your number was 3.25, the calculation would be 3.25 - 3. This will give 0.25

Getting the fractional part of a double value in integer without losing precision

i want to convert the fractional part of a double value with precision upto 4 digits into integer. but when i do it, i lose precision. Is there any way so that i can get the precise value?
#include<stdio.h>
int main()
{
double number;
double fractional_part;
int output;
number = 1.1234;
fractional_part = number-(int)number;
fractional_part = fractional_part*10000.0;
printf("%lf\n",fractional_part);
output = (int)fractional_part;
printf("%d\n",output);
return 0;
}
i am expecting output to be 1234 but it gives 1233. please suggest a way so that i can get desired output. i want the solution in C language.
Assuming you want to get back a positive fraction even for negative values, I'd go with
(int)round(fabs(value - trunc(value)) * 1e4)
which should give you the expected result 1234.
If you do not round and just truncate the value
(int)(fabs(value - trunc(value)) * 1e4)
(which is essentially the same as your original code), you'll end up with the unexpected result 1233 as 1.1234 - 1.0 = 0.12339999999999995 in double precision.
Without using round(), you'll also get the expected result if you change the order of operations to
(int)(fabs(value * 1e4 - trunc(value) * 1e4))
If the integral part of value is large enough, floating-point inaccuracies will of course kick in again.
You can also use modf() instead of trunc() as David suggests, which is probably the best approach as far as floating point accuracy goes:
double dummy;
(int)round(fabs(modf(value, &dummy)) * 1e4)
number= 1.1234, whole=1, fraction=1234
int main()
{
double number;
int whole, fraction;
number = 1.1234;
whole= (int)number;
fraction =(int)(number*10000);
fraction = fraction-(whole *10000);
printf("%d\n",fraction);
printf("%d\n",whole);
return 0;
}
A solution for any number could be:
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float number = 123.46244;
float number_final;
float temp = number; // keep the number in a temporary variable
int temp2 = 1; // keep the length of the fractional part
while (fmod(temp, 10) !=0) // find the length of the fractional part
{
temp = temp*10;
temp2 *= 10;
}
temp /= 10; // in tins step our number is lile this xxxx0
temp2 /= 10;
number_final = fmod(temp, temp2);
cout<<number_final;
getch();
return 0;
}
Use modf and ceil
#include <stdio.h>
#include <math.h>
int main(void)
{
double param, fractpart, intpart;
int output;
param = 1.1234;
fractpart = modf(param , &intpart);
output = (int)(ceil(fractpart * 10000));
printf("%d\n", output);
return 0;
}

How to convert floating value to integer with exact precision like 123.3443 to 1233443?

Sample code:
int main() {
float f = 123.542;
int i = (int)f;
printf("%d\n",i);
}
123.3443 can't be exactly represented by a floating-point number -- in a 32-bit float, it's effectively represented as 16166984 / 131072, which is actually 123.34429931640625, not 123.3443. (It's off by around 6.8 x 10^-7.)
If this is really the result you want (which it's probably not), take a look at how IEEE-754 floats work, and pull out your favorite arbitrary-precision math suite. Once you understand what's going on "behind the scenes", generating an exact representation shouldn't be too hard. Generating a "close enough" rounded representation is actually much harder. :)
int i = (int) (f * 10000 + 0.5);
Multiply the float by 10^x where x is how many digits after the decimal you want and then cast to int.
If you don't need to do any repetitive processing of the converted number and are only looking to convert the numbers into an integer, the easiest way is to use sprintf, then sum up the decimals with a for loop using power of 10 rules. If you need "exact precision" for math, use a BCD. The following algorithm will allow you to truncate the number of digits for "exact precision".
#include "math.h"
long ConvertToScaledInteger (float value, int significantDigits = -1)
{
// Note: for 32-bit float, use long return and for double use long long.
if (significantDigits < 1) // Ditch the '-' and the '.'
significantDigits += 2;
else
++significantDigits; // Ditch the '.'
char* floatingPointString = malloc(sizeof (char) * (4 + FLT_MANT_DIG - FLT_MIN_EXP));
/*< This solution is for a 32-bit floating point number. Replace FLT
with DBL, and it will produce the max number of chars in a 64-bit
float string. */
if (significantDigits < 0) //< Then use all of the float's digits.
{
sprintf (floatingPointString , "%f", floatingPointNumber);
}
else //< Then truncate the number of decimal places.
{
char decimalPlaceString[9];
char percentString = "%\0";
sprintf (decimalPlaceString, "%s%if", percentString , significantDigits);
sprintf (floatingPointString , decimalPlaceString, value);
}
int stringLength = strlen (floatingPointString),
index;
long returnValue = 0;
double powerOfTen = 10.0,
currentValue;
// Find if we have an ending of .0, and backtrack.
if (floatingPointString[stringLength - 1] == '0' && floatingPointString[stringLength - 2] == '.')
index = stringLength - 3;
else
index = stringLength - 1;
for (; index > 0; --index)
{
if (floatingPointString[index] == '.')
--index;
// Subtract ASCII '0' converts ASCII to integer.
currentValue = (double) floatingPointString[index] - '0';
returnValue += (long) currentValue * pow (10.0, powerOfTen);
powerOfTen += 1.0;
}
if (floatingPointString[0] == '-')
returnValue *= -1;
return returnValue;
}
The integer equivalent of a float number f
float f=123.456;
can be made using modff() like
float integral, fractional;
char str[50], temp[20];
fractional = modff(f, &integral);
Now integral has the integer part (like 123.000000) and fractional has the fractional part (like 0.456000).
If the floating point number (f in this case) were negative, both integral and fractional would be negative.
You can do
if(fractional<0)
{
fractional = -fractional;
}
to remedy that.
Now,
sprintf(temp, "%g", fractional);
The %g format specifier will remove the trailing zeros and temp will now have "0.456".
sprintf(str, "%g%s", integral, temp[1]=='.'?temp+2:"");
The temp[1]=='.'? is done because if the fractional part were 0, there would'v been no decimal point while printing fractional as it would've been 0 and not 0.000000. If the second character in temp is not ., fractional is zero and we needn't bother with it.
Now in our case, str would be "123456". But that's in the form of a string. We need to convert it into an integer. Use strtol() for that.
long l=strtol(str, NULL, 10);
if(l>INT_MAX || errno==ERANGE)
{
printf("\noverflow");
}
else
{
printf("\n%d", strtol(str, NULL, 10));
}
You may check the return value of strtol() and that of errno (from errno.h. Check whether it is ERANGE ) to see if overflow occurred.
To see if the resultant value can be stored in an int, first store the value returned by strtol() in a long int and see if that is greater than INT_MAX (it's in limits.h).
It should be noted that the accuracy of the result will depend upon the accuracy with which the floating point number is represented in binary.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int f2i(float v, int size, int fsize){
char *buff, *p;
int ret;
buff = malloc(sizeof(char)*(size + 2));
sprintf(buff, "%*.*f", size, fsize, v);
p = strchr(buff, '.');
while(*p=*(p+1))p++;
ret = atoi(buff);
free(buff);
return ret;
}
int main(){
float f = 123.3443;
printf("%d\n", f2i(f, 7, 4));
f=123.542;
printf("%d\n", f2i(f, 6, 3));
return 0;
}

How to extract the decimal part from a floating point number in C?

How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables?
You use the modf function:
double integral;
double fractional = modf(some_double, &integral);
You can also cast it to an integer, but be warned you may overflow the integer. The result is not predictable then.
Try this:
int main() {
double num = 23.345;
int intpart = (int)num;
double decpart = num - intpart;
printf("Num = %f, intpart = %d, decpart = %f\n", num, intpart, decpart);
}
For me, it produces:
Num = 23.345000, intpart = 23, decpart = 0.345000
Which appears to be what you're asking for.
The quick "in a nut shell" most obvious answer seems like:
#define N_DECIMAL_POINTS_PRECISION (1000) // n = 3. Three decimal points.
float f = 123.456;
int integerPart = (int)f;
int decimalPart = ((int)(f*N_DECIMAL_POINTS_PRECISION)%N_DECIMAL_POINTS_PRECISION);
You would change how many decimal points you want by changing the N_DECIMAL_POINTS_PRECISION to suit your needs.
I created a subroutine one using a double float, it returns 2 integer values.
void double2Ints(double f, int p, int *i, int *d)
{
// f = float, p=decimal precision, i=integer, d=decimal
int li;
int prec=1;
for(int x=p;x>0;x--)
{
prec*=10;
}; // same as power(10,p)
li = (int) f; // get integer part
*d = (int) ((f-li)*prec); // get decimal part
*i = li;
}
void test()
{
double df = 3.14159265;
int i,d;
for(int p=2;p<9;p++)
{
double2Ints(df, p, &i,&d); printf("d2i (%d) %f = %d.%d\r\n",p, df,i,d);
}
}
I think that using string is the correct way to go in this case, since you don't know a priori the number of digits in the decimal part. But, it won't work for all cases (e.g. 1.005), as mentioned before by #SingleNegationElimination. Here is my take on this:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s_value[60], s_integral[60], s_fractional[60];
int i, found = 0, count = 1, integral, fractional;
scanf("%s", s_value);
for (i = 0; s_value[i] != '\0'; i++)
{
if (!found)
{
if (s_value[i] == '.')
{
found = 1;
s_integral[i] = '\0';
continue;
}
s_integral[i] = s_value[i];
count++;
}
else
s_fractional[i - count] = s_value[i];
}
s_fractional[i - count] = '\0';
integral = atoi(s_integral);
fractional = atoi(s_fractional);
printf("value = %s, integral = %d, fractional = %d\n",
s_value, integral, fractional);
return 0;
}
Use the floating number to subtract the floored value to get its fractional part:
double fractional = some_double - floor(some_double);
This prevents the typecasting to an integer, which may cause overflow if the floating number is very large that an integer value could not even contain it.
Also for negative values, this code gives you the positive fractional part of the floating number since floor() computes the largest integer value not greater than the input value.
Here is another way:
#include <stdlib.h>
int main()
{
char* inStr = "123.4567"; //the number we want to convert
char* endptr; //unused char ptr for strtod
char* loc = strchr(inStr, '.');
long mantissa = strtod(loc+1, endptr);
long whole = strtod(inStr, endptr);
printf("whole: %d \n", whole); //whole number portion
printf("mantissa: %d", mantissa); //decimal portion
}
http://codepad.org/jyHoBALU
Output:
whole: 123
mantissa: 4567
float num;
int intgPart;
float fracPart;
printf("Enter the positive floating point number: ");
scanf("%f", &num);
intgPart = (int)num;
fracPart = num - intgPart;
The fractional part can be obtained by subtracting an integral part from the original double value.
Maybe the best idea is to solve the problem while the data is in String format. If you have the data as String, you may parse it according to the decimal point. You extract the integral and decimal part as Substrings and then convert these substrings to actual integers.
I made this function, it seems to work fine:
#include <math.h>
void GetFloattoInt (double fnum, long precision, long *pe, long *pd)
{
long pe_sign;
long intpart;
float decpart;
if(fnum>=0)
{
pe_sign=1;
}
else
{
pe_sign=-1;
}
intpart=(long)fnum;
decpart=fnum-intpart;
*pe=intpart;
*pd=(((long)(decpart*pe_sign*pow(10,precision)))%(long)pow(10,precision));
}
If you just want to get the first decimal value, the solution is really simple.
Here's an explanatory example:
int leftSideOfDecimalPoint = (int) initialFloatValue; // The cast from float to int keeps only the integer part
int temp = (int) initialFloatValue * 10;
int rightSideOfDecimalPoint = temp % 10;
Say for example we have an initial float value of 27.8 .
By just casting the initial float value to an int, you discard the fraction part and only keep the integer part.
By multiplying the initial float value by 10 we get a result of 278.0, then by casting this result to int, gives you the value of 278
If we divide 278 by 10, we get 27.8, of which the remainder is 8, which is the value at the right side of the decimal point. Thus use modulus.
This technique can then be used to get the following decimal characters by using for example 100 instead of 10, and so on.
Just take note that if you use this technique on real-time systems, for example to display it on a 7-segment display, it may not work properly because we are multiplying with a float value, where multiplication takes a lot of overhead time.
#include <stdio.h>
Int main ()
{
float f=56.75;
int a=(int)f;
int result=(f-a)*100;
printf ("integer = %d\n decimal part to integer
=%d\n",result);
}
Output:-
integer =56
decimal part to integer = 75
Suppose A is your integer then (int)A, means casting the number to an integer and will be the integer part, the other is (A - (int)A)*10^n, here n is the number of decimals to keep.
My printf() didn't support formatting for floats. This was my solution to print it as two integers. Change 100 in to what you like to increase the precision.
I found this one
#define DEC(f) (uint32_t)(100*((f)-(float)((uint32_t)(f))))
#define INT(f) (uint32_t)(f)
DEC(3.1416..) = 14 (int)
INT(3.1416..) = 3 (int)
Let me know what you think of this solution.
Even I was thinking how to do it. But I found a way.
Try this code
printf("Enter a floating number");
scanf("%d%c%d", &no, &dot, &dec);
printf("Number=%d Decimal part=%d", no, dec);
Output:-
Enter a floating number
23.13
Number=23 Decimal part=13
cout<<"enter a decimal number\n";
cin>>str;
for(i=0;i<str.size();i++)
{
if(str[i]=='.')
break;
}
for(j=i+1;j<str.size();j++)
{
cout<<str[j];
}

Resources