gpt4 book ai didi

c - 实现了名为 atof_beta 的 atof,但结果与 atof 不同

转载 作者:行者123 更新时间:2023-11-30 15:24:42 27 4
gpt4 key购买 nike

我写了一个atof函数,名为atof_beta。我运行它,但结果与atof的结果不一样。

例如:输入:1111111111111111111111111111111111111111111111111111111111111111111111111111111111

输出:(fabs(val-atof(s)) <= EPSILON) 等于 0。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <math.h>

double check_overflow(double val)
{
if (val > DBL_MAX){
return DBL_MAX;
}else if (val < DBL_MIN){
return DBL_MIN;
}
return val;
}


double get_exp(char *s)
{
double exp = 10;
int num = 0x00;
char c = *s;

if (c == '-'){
exp = 0.1;
s++;
}else if (c == '+'){
s++;
}

while ((c = *s++) != '\0'){
if ((c >= '0') && (c <= '9')){
num = num*10 + c - '0';
}
else{
return 0x01;
}
}

double tmp = 0x01;
while (num--){
tmp *= exp;
}

return tmp;
}

double atof_beta(char *s)
{
double integer = 0x00;
double fraction = 0x00;
int sign = 0x01;
int fraction_flag = 0x00;
double divisor = 0x01;
double exp = 0x01;

char c = *s;

if (c == '-'){
sign = -1;
s++;
}else if (c == '+'){
sign = 1;
s++;
}

while ((c = (*s++)) != '\0'){
if ((c >= '0') && (c <= '9')){
if (!fraction_flag){
integer = integer*10 + (c -'0');
}else{
fraction = fraction*10 + c -'0';
divisor *= 10;
}
}else if (c == '.'){
fraction_flag = 0x01;
}else if ((c == 'e') || (c == 'E')){
exp = get_exp(s);
break;
}else{
break;
}
}

integer = check_overflow(integer);
fraction = check_overflow(fraction);
exp = check_overflow(exp);
double val = sign*((integer + (fraction/divisor))*exp);
return check_overflow(val);
}

#define EPSILON 1e-6
void main()
{
char s[1024], *p;
double val;

while(1)
{
printf("enter string : ");
fgets(s, sizeof(s), stdin);
if ((p = strchr(s, '\n')) != NULL) {
*p = '\0'; /* Remove newline */
}
puts(s); /* No need to cast */
val = atof_beta(s);
printf("\nfinal value : %lf, %lf, %d\n", val, atof(s), (fabs(val-atof(s)) <= EPSILON));
}

return;
}

最佳答案

这是错误的:

char *s;
...
printf("enter string : ");
s = (char *)malloc(sizeof(char));
gets((char *)s);
puts((const char *)s);

你为单个char保留空间,但你想要一个字符串,还要注意gets在C99中已被弃用,C11删除了它,我建议fgets:

char s[32], *p;
...
printf("enter string : ");
/* No need to malloc */
fgets(s, sizeof s, stdin);
if ((p = strchr(s, '\n')) != NULL) {
*p = '\0'; /* Remove newline */
}
puts(s); /* No need to cast */

关于c - 实现了名为 atof_beta 的 atof,但结果与 atof 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28292633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com