C string to float conversion [duplicate] - c

This question already has answers here:
Converting string, to float (without atof) in C
(11 answers)
Closed 6 years ago.
I need to convert a string such as "3.456" to a float without atof() or any other string manipulation functions. Below is my code that works for a c string to int conversion but obviously does not work with a float because of the period. What do I need to add to deal with the period? Thank you.
#include <stdio.h>
int size;
char string[] = "345";
int result = 0;
int i = 0;
int findLength(char string[]){
for(size = 0; string[size]!='\0'; size++){
}
return size;
};
void conversion(char string[]){
result = result * 10 + ( string[i] - '0' );
if (i < size - 1) {
i++;
conversion(string);
}
}
int main(int argc, const char * argv[]) {
findLength(string);
conversion(string);
printf("%d\n", result);
return 0;
}

Before anything else, I think it's important for you to understand how floating point numbers work and how they are represented, at least in C. Look at this question/reply for more information.
Now back to your question. Because of how float are represented in memory, you might not be able to get an exact conversion. That is you might not be able to convert "3.456" to exactly a float that holds the value 3.456. You will only be able to get close to the exact value you want.
First a couple of things wrong with your code:
I would stay away from using global variables in my code unless necessary. You defined "size" as a global variable and that's not good practice.
Use the standard library strlen function to get the length of a string instead of rewriting a function that does so.
Below, you'll find a working solution. The way to do the conversion is as follow:
Scan through the string to get the position of the decimal point
If a non-dot character is encountered, add it to the result
When you'r done scanning through the string, you need to account for the power of the digits before the decimal point. For example, the first digit before the decimal point is simply added to the result. The second digit before the decimal point is multiplied by 10.0 and added to the result and so on. That is accomplished with a simple while loop as shown in the code.
#include <stdio.h>
#include <string.h>
float stringToFloat(char *string)
{
float result= 0.0;
int len = strlen(string);
int dotPosition = 0;
for (int i = 0; i < len; i++)
{
if (string[i] == '.')
{
dotPosition = len - i - 1;
}
else
{
result = result * 10.0 + (string[i]-'0');
}
}
while (dotPosition--)
{
result /= 10.0;
}
return result;
}
int main()
{
char string[] = "3.456";
printf("%f\n",stringToFloat(string));
}
I hope this helps.
P.S: You could increase the precision by using a double instead of a float.

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.

While trying to write the recursive version of atoi, what is the meaning of itoa(n /10, s) and why is it advisable to avoid static int i = 0?

I'm sorry if my question is quite vague, because it is without context. So I was trying to solve a question: Write a recursive version of the function itoa(i) which converts an integer i into a string.
As I ran out of idea how to solve it, I started looking online to find solutions and I came across some of them which usually use this line: itoa(n/10, s);. For example, from this StackOverflow question: itoa recursively. I can't understand what it does to i.
So I went on and searched for more solutions and I found one that actually works, the program looks like this:
#include <stdio.h>
char *itoa(int n, char s[]);
int main()
{
char number[100];
printf("-90 to string = %s\n", itoa(-90, number));
return 0;
}
char *itoa(int n, char s[])
{
static int i = 0;
if (n < 0) {
s[i++] = '-';
n = -n; /* does not work for largest negative number in two complement */
}
if (n / 10)
itoa(n /10, s);
s[i++] = n % 10 + '0';
s[i] = '\0';
return s;
}
Problem is, according to the solutions I found on other websites, people said that we should avoid using static int i. I did not read why we should do so because I don't know how static works so I don't know if this program is fine or needs improvements.
Your function pretty much almost right, that is for a recursive method. If you were going to parse the digit to string backward, it is right. Otherwise, I just did a couple fix.
For parsing digit to string, the digits that are being parsed are from the right digits to left digits as the remainder is what being used. Thus, when storing those into a string, we will need to go from high to low indexes. If we use remainders for parsing, we will not know the length of the number that is being parsed. Thus, in most parse cases there will be some extra spaces at the beginning of your string or char array.
For using the static i, you can pass a version of it around but it would make it harder to use as you would need to know to always have to pass i at 11. "i" is at 11 because the maximum digits for an int is 10(digits) + 1 sign and the 12th character which not counted by "i" is the null char. To make it easier to use the function, I configured the third parameter to be a void pointer. However, do not pass it an actual pointer, pass it a NULL. When it see NULL as the third parameter, it know that, that is the first call.
#include <stdio.h>
#include <string.h>
char *itoa(int n, char s[], void * );
int main()
{
char number[100] = {0};
printf("-90 to string = %s\n", itoa(154, number, NULL));
printf("-15 to string = %s\n", itoa(-15, number, NULL));
printf("-2147483648 to string = %s\n", itoa(-2147483648, number, NULL));
return 0;
}
// The reason why I pass a void pointer is because
// instead of passing the value is because it easier to use function without needing to know that the string have to go from right left.
// When you first call it just use NULL as the third parameter. Anything else can ruin it.
char *itoa(int n, char s[], void * firstCall )
{
static int i;
if ( firstCall == NULL ) {
i = 11;
s[11] = 0;
}
int neg = 0;
if (n < 0) {
if ( n == -2147483648 ) {
strcpy(s, "-2147483648");
return s;
}
neg=1;
n = -n; /* does not work for largest negative number in two complement */
}
s[--i] = (n % 10) ^ 48;
if ( n / 10 ) itoa(n / 10, s, s);
if ( neg == 1 ) s[--i] = '-';
return &s[i];
}

how to fix this code (conversion binary to decimal with char in c)?

why this code don't work? (i want To convert a number from binary to decimal)
there is a problem with pointer? or multiplication(char*int)
char cara[100];
char *t=cara;
int i=0;
int sum=0;
int j=0;
int m;
printf("entrer a binary number\n");
scanf("%s",&cara);
while(*t!="\0")
{
j++;
m=j-1;
}
for(i=0;i<j;i++)
{
cara[i]=cara[i]*pow(2,m);
m--;
}
int k;
for(k=0;k<j;k++)
{
sum=sum+cara[i];
}
printf("%d",sum);
}
The first bug is here:
while(*t!="\0")
You have to use single quotes for characters - like while(*t!='\0'). To make it simpler you can just write while(*t).
Besides that problem also notice that you never update t. You need to add ++t; inside the loop.
The second bug is here:
cara[i]=cara[i]*pow(2,m);
Characters '0' and '1' do not have integer values zero and one so the calculation is wrong. The character '0' has integer value 48 and character '1' has integer value 49 (see https://da.wikipedia.org/wiki/ASCII) so the result will be completely wrong.
To get the calculation more correct you need:
cara[i] = (cara[i] - '0') * pow(2,m);
However, it is a bad idea to save these intermediate results back in a char. A char can't hold values higher than 127 (or 255 if char is unsigned) so you'll soon have an integer overflow.
In general there is no need for saving these intermediate result and there is no need for pow as you can simply multiply by 2 for each binary digit.
Try like this:
#include <stdio.h>
int main(void) {
char *bin_str = "11001";
unsigned result = 0;
while (*bin_str)
{
result *= 2;
result += *bin_str == '1' ? 1 : 0;
++bin_str;
}
printf("%d\n", result);
return 0;
}

Problems in Project Euler #8

I have been working on Project Euler lately. I am programming in C and have a query related to Problem 8. The question is:
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Here is my own done work yet:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "731671765313306249192251196744.............";
char *x;
x = string;
for(int counter = 0;counter < 2;counter++)
{
printf("%s\n", x + strlen(x) - 5);
x = x + strlen(x) - 5;
}
}
SO, the problem is that unable to understand what to do next. I am able to extract the digits, but what would be the algorithm to use these digits and create an algorithm thats fast and efficient. So, any help, please! Another problem is that when I try to convert the char to an int value, it fails and gives the error "error: incompatible pointer to integer conversion initializing 'char' with an expression of type'char[1001]'. I have used the methods to convert values as here:Coversion of char to int, and so thats it. If any more suggestions, please suggest the better things...
You can read the characters in the string, convert them to integers and then find their product to check which group of 5 consecutive integers give the highest product. At the loss of fun and learning that you can have trying to do it yourself, you can try this.
#include <stdio.h>
#include <string.h>
// calculates the product of a group of numeric chars
// after converting them to int type
int gproduct(char *s, int glen) {
int i = 0;
int val = 1;
// convert a numeric char to int value by subtracting '0'
// assuming the chars are encoded in ascii. consider using
// the standard library function atoi for portability.
while(i < glen)
val *= (s[i++] - '0');
return val;
}
int main(void) {
char *numstr = "731671765313306249192251196744.............";
int glen = 5; // group length
int maxcount = strlen(numstr) - glen;
int maxval = gproduct(numstr, glen); // initialize maxval
int tempval;
for(int i = 1; i < maxcount; i++) {
tempval = gproduct(numstr + i, glen);
if(tempval > maxval)
maxval = tempval;
}
printf("%d\n", maxval);
return 0;
}

K&R Exercise 2-3 "Hex to int converter" Problem

The program I wrote works in demographics consisting of only single Hexadecimal values. (Probably not the most elegant solution, but I'm a new programmer) My question is, how would I go about handling of multiple hexadecimal digits, such as 0xAF, or 0xFF, etc? I'm not exactly sure, and I've seemed confuse myself greatly, in the attempt. I'm not asking for someone to hold my hand, but to give me a tip where I've gone wrong in this code and thoughts on how to fix it.
Thanks :)
/* Exercise 2-3. Write the function htoi(s), which converts a string of
* hexadecimal digits (including an optional 0x or 0X) into it's equivalent
* integer value. The allowable digits are 0...9 - A...F and a...f.
*
*/
#include <stdio.h>
#include <string.h>
#define NL '\n'
#define MAX 24
int htoi(char *hexd);
int
main(void)
{
char str[MAX] = {0};
char hex[] = "0123456789ABCDEFabcdef\0";
int c;
int i;
int x = 0;
while((c = getchar()) != EOF) {
for(i = 0; hex[i] != '\0'; i++) {
if(c == hex[i])
str[x++] = c;
}
if(c == NL) {
printf("%d\n", htoi(str));
x = 0, i = x;
}
}
return 0;
}
int
htoi(char *hexd)
{
int i;
int n = 0;
for(i = 0; isdigit(hexd[i]); i++)
n = (16 * i) + (hexd[i] - '0');
for(i = 0; isupper(hexd[i]); i++) /* Let's just deal with lowercase characters */
hexd[i] = hexd[i] + 'a' - 'A';
for(i = 0; islower(hexd[i]); i++) {
hexd[i] = hexd[i] - 'a';
n = (16 + i) + hexd[i] + 10;
n = hexd[i] + 10;
}
return n;
}
Someone has alredy asked this (hex to int, k&r 2.3).
Take a look, there are many good answers, but you have to fill in the blanks.
Hex to Decimal conversion [K&R exercise]
Edit:
in
char hex[] = "0123456789ABCDEFabcdef\0";
The \0 is not necesary. hex is alredy nul terminated. Is len (0...f) + 1 = 17 bytes long.
I'll pick on one loop, and leave it to you to rethink your implementation. Specifically this:
for(i = 0; isdigit(hexd[i]); i++)
n = (16 * i) + (hexd[i] - '0');
doesn't do what you probably think it does...
It only processes the first span of characters where isdigit() is TRUE.
It stops on the first character where isdigit() is FALSE.
It doesn't run past the end because isdigit('\0') is known to be FALSE. I'm concerned that might be accidentally correct, though.
It does correctly convert a hex number that can be expressed solely with digits 0-9.
Things to think about for the whole program:
Generally, prefer to not modify input strings unless the modification is a valuable side effect. In your example code, you are forcing the string to lower case in-place. Modifying the input string in-place means that a user writing htoi("1234") is invoking undefined behavior. You really don't want to do that.
Only one of the loops over digits is going to process a non-zero number of digits.
What happens if I send 0123456789ABCDEF0123456789ABCDEF to stdin?
What do you expect to get for 80000000? What did you get? Are you surprised?
Personally, I wouldn't use NL for '\n'. C usage pretty much expects to see \n in a lot of contexts where the macro is not convenient, so it is better to just get used to it now...
I think that the MAX size of string should be either 10 or 18 instead of 24. (If you have already checked the int on your machine and followed the reasoning bellow, it would be beneficial to include it as a comment in your code.)
10 : since htoi() returns an int , int is usually 4 bytes (check your system's too), so the hexadecimal number can be atmost 8 digits in length (4bits to 1 hex digit, 8 bits to a byte), and we want to allow for the optional 0x or 0X.
18 : would be better if htoi() returned a long and its 8 bytes (again, check your system's), so the hexadecimal number can be atmost 16 digits in length, and we want to allow for the optional 0x or 0X.
Please note that that sizes of int and long are machine dependent, and please look at exercise 2.1 in the K&R book to find them.
Here is my version of a classic htoi() function to convert multiple hexadecimal values into decimal integers. It's a full working program compile it and run.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int htoi(const char*);
int getRawInt(char);
int main(int argc, char **argv) {
char hex[] = " ";
printf("Enter a hexadecimal number (i.e 33A)\n");
scanf("%s", hex);
printf("Hexedecimal %s in decimal is %d\n", hex, htoi(hex)); // result will be 826
return 0;
}
int htoi(const char *hex) {
const int LEN = strlen(hex) -1;
int power = 1;
int dec = 0;
for(int i = LEN; i >= 0; --i) {
dec += getRawInt(hex[i]) * power;
power *= 16;
}
return dec;
}
int getRawInt(char c) {
if(isalpha(c)) {
return toupper(c) - 'A' + 10;
} return c-'0';
}

Resources