bugfix: 修复浮点数打印错误的bug

This commit is contained in:
fslongjin
2022-07-12 13:19:51 +08:00
parent 676260c537
commit 7670031b11
16 changed files with 235 additions and 35 deletions

View File

@ -1,6 +1,8 @@
#include <libc/stdio.h>
#include <libc/stdlib.h>
#include <libc/unistd.h>
#include <libc/time.h>
#include <libc/math.h>
void print_ascii_logo()
{
@ -26,11 +28,11 @@ int main()
{
// printf("Hello World!\n");
print_ascii_logo();
usleep(500000);
usleep(500000);
print_copyright();
// exit(0);
// while (1)
// ;
return 0;
}

View File

@ -1,5 +1,5 @@
#pragma once
#include "stddef.h"
double fabs(double x);
float fabsf(float x);
@ -7,4 +7,6 @@ long double fabsl(long double x);
double round(double x);
float roundf(float x);
long double roundl(long double x);
long double roundl(long double x);
int64_t pow(int64_t x, int y);

View File

@ -1,11 +1,14 @@
all: fabs.o round.o
CFLAGS += -I .
all: fabs.o round.o pow.o
fabs.o: fabs.c
gcc $(CFLAGS) -c fabs.c -o fabs.o
round.o: round.c
gcc $(CFLAGS) -c round.c -o round.o
gcc $(CFLAGS) -c round.c -o round.o
pow.o: pow.c
gcc $(CFLAGS) -c pow.c -o pow.o

View File

@ -1,6 +1,7 @@
#include <libc/math.h>
#include <libc/sys/types.h>
#include "libm.h"
double fabs(double x)
{
union

10
user/libs/libc/math/pow.c Normal file
View File

@ -0,0 +1,10 @@
#include "math.h"
#include <libc/stddef.h>
int64_t pow(int64_t x, int y)
{
int64_t res = 1;
for (int i = 0; i < y; ++i)
res *= x;
return res;
}

View File

@ -9,7 +9,6 @@
static char *write_num(char *str, uint64_t num, int base, int field_width, int precision, int flags);
static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
static int skip_and_atoi(const char **s)
{
/**
@ -323,10 +322,6 @@ int vsprintf(char *buf, const char *fmt, va_list args)
break;
case 'f':
// 默认精度为3
// printk("1111\n");
// va_arg(args, double);
// printk("222\n");
if (precision < 0)
precision = 3;
@ -497,11 +492,11 @@ static char *write_float_point_num(char *str, double num, int field_width, int p
if (sign)
--field_width;
int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度
uint64_t num_z = (uint64_t)(num); // 获取整数部分
uint64_t num_decimal = (uint64_t)(round((num - num_z) * precision)); // 获取小数部分
int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度
uint64_t num_z = (uint64_t)(num); // 获取整数部分
uint64_t num_decimal = (uint64_t)(round(1.0*(num - num_z) * pow(10, precision))); // 获取小数部分
if (num == 0)
if (num == 0 || num_z == 0)
tmp_num_z[js_num_z++] = '0';
else
{
@ -530,18 +525,23 @@ static char *write_float_point_num(char *str, double num, int field_width, int p
*str++ = sign;
// 输出整数部分
while (js_num_z-- > 0)
*str++ = tmp_num_z[js_num_z];
// while (js_num_z-- > 0)
// *str++ = tmp_num_z[js_num_z];
while (js_num_z > 0)
{
*str++ = tmp_num_z[js_num_z - 1];
--js_num_z;
}
*str++ = '.';
// 输出小数部分
while (js_num_d-- > 0)
int total_dec_count = js_num_d;
for (int i = 0; i < precision && js_num_d-- > 0; ++i)
*str++ = tmp_num_d[js_num_d];
while (js_num_d < precision)
while (total_dec_count < precision)
{
--precision;
++total_dec_count;
*str++ = '0';
}