美文网首页C语言&嵌入式嵌入式
嵌入式中浮点数转字符串函数

嵌入式中浮点数转字符串函数

作者: FSS_Sosei | 来源:发表于2019-01-12 21:30 被阅读0次

2012年写的

原来放在开源中国社区的

还是转过来吧

AVR-GCC平台里面没有浮点转字符串

我写了个。特别的地方是末位舍入用随机舍入法

/*

    <Floating number to string>

    Copyright (C) <2012>  <fss.sosei>

    This program is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License

    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

doublerandom_round (doublex, int_fast8_t rounded_to_exp10)

{

  constuint_fast8_t MAX_SIGNIFICANT_FIGURES = 5;  //float floor(log10(pow(2, 23)))-1, double floor(log10(pow(2, 52)))-1

  if(not isfinite(x))

  {

    returnx;

  }

  doubleabsolute_value = fabs(x);

  if((floor(log10(absolute_value)) - rounded_to_exp10) >= MAX_SIGNIFICANT_FIGURES)

  {

    returnx;

  }

  doubleinteger_part, decimal_part;

  decimal_part = modf(absolute_value * pow(10, -rounded_to_exp10), &integer_part);

  if(decimal_part > 0.5)

  {

    integer_part += 1;

  }

  else

  {

    if(decimal_part == 0.5)

    {

      integer_part += rand() % 2;

    }

  }

  returncopysign((integer_part + 0.1) * pow(10, rounded_to_exp10), x);

}

uint_fast8_t floating_number_to_string (doubleinput_number, uint_fast8_t decimal_digits, charnumeric_string[], uint_fast8_t array_len)  //input_number range is 99999 to 0.000001. Returns the numeric string length. The random roundoff, pay attention to the random number seed.

{

  constuint_fast8_t MAX_SIGNIFICANT_FIGURES = 5;  //float floor(log10(2^23))-1

  constuint_fast8_t MIN_DECIMAL = 6;

  chartemp_string[10];  //a sign + (decimal_digits == 0 ? MAX_SIGNIFICANT_FIGURES : max((MAX_SIGNIFICANT_FIGURES + a decimal), (a zero + a decimal + MIN_DECIMAL))) + '\x0'

  doubleabsolute_value;

  doublenum_exp10;

  if(not isfinite(input_number))

  {

    return0;

  }

  if(decimal_digits > MIN_DECIMAL)

  {

    return0;

  }

  absolute_value = fabs(input_number);

  num_exp10 = floor(log10(absolute_value));

  if(num_exp10 >= MAX_SIGNIFICANT_FIGURES)

  {  //Value ultra range error

    return0;

  }

  doublenum_tail_exp10 = fmax((num_exp10 + 1 - MAX_SIGNIFICANT_FIGURES), (-((int_fast8_t)decimal_digits)));  //Plus integer part

  input_number = random_round(absolute_value, lround(num_tail_exp10));

  absolute_value = fabs(input_number);

  num_exp10 = floor(log10(absolute_value));

  int_fast8_t num_int_exp10 = lround(num_exp10);

  if(num_int_exp10 >= MAX_SIGNIFICANT_FIGURES)

  {  //Value ultra range error

    return0;

  }

  num_tail_exp10 = fmax((num_exp10 + 1 - MAX_SIGNIFICANT_FIGURES), (-((int_fast8_t)decimal_digits)));  //Plus integer part

  int_fast8_t num_int_tail_exp10 = lround(num_tail_exp10);

  if(num_int_tail_exp10 > num_int_exp10)

  {

    num_exp10 = num_tail_exp10;

    num_int_exp10 = num_int_tail_exp10;

  }

  uint_fast8_t num_count = num_int_exp10 - num_int_tail_exp10 + 1;

  doubleintermediate_number = absolute_value * pow(10, -num_exp10);

  doubledigital_character[num_count];

  for(int_fast8_t i = 0; i < num_count; ++i)

  {

    intermediate_number = modf(intermediate_number, &digital_character[i]) * 10;

  }

  int_fast8_t i = 0;

  uint_fast8_t n;

  if((input_number < 0 ? 1 : 0) == 1)

  {

    temp_string[i] = '-'; ++i;

  }

  int_fast8_t j = 0;

  if(num_int_exp10 >= 0)

  {

    n = i + num_int_exp10;

    while(i <= n)

    {

      temp_string[i] = (char)(lround(digital_character[j]) + 0x30);  //0x30 == '0'

      ++i;

      ++j;

    }

    if((num_int_tail_exp10 < 0 ? 1 : 0) == 1)

    {

      temp_string[i] = '.'; ++i;

    }

    n = i + abs(num_int_tail_exp10);

    while(i < n)

    {

      temp_string[i] = (char)(lround(digital_character[j]) + 0x30);  //0x30 == '0'

      ++i;

      ++j;

    }

  }

  else

  {

    temp_string[i] = '0'; ++i;

    temp_string[i] = '.'; ++i;

    n = i + (-1 - num_int_exp10);

    while(i < n)

    {

      temp_string[i] = '0'; ++i;

    }

    n = i + (num_int_exp10 - num_int_tail_exp10);

    while(i <= n)

    {

      temp_string[i] = (char)(lround(digital_character[j]) + 0x30);  //0x30 == '0'

      ++i;

      ++j;

    }

  }

  temp_string[i] = '\x0';

  n = i;

  if((n + 1) > array_len)

  {

    return0;

  }

  for(i=0; i <= n; ++i)

  {

    numeric_string[i] = temp_string[i];

  }

  returnn;  //Returns the numeric string length

}

相关文章

  • 嵌入式中浮点数转字符串函数

    2012年写的 原来放在开源中国社区的 还是转过来吧 AVR-GCC平台里面没有浮点转字符串 我写了个。特别的地方...

  • Presto优雅之道

    作者:邱晨 一、常用函数 数值相关字符串转浮点数、整数select cast (bj_score as bigin...

  • python开发--数字

    数字中整数与浮点数的使用,以及将非字符串值转为字符串(使用str()函数) 返回数据

  • PHP数据类型字符串、整数、浮点数、逻辑、数组、对象、NULL

    整数 浮点数 NULL PHP字符串常用函数 strlen() 函数返回字符串的长度,以字符计。 strpos()...

  • 2018-07-03

    parseFloat() 函数可解析一个字符串,并返回一个浮点数。该函数指定字符串中的首个字符是否是数字。如果是,...

  • random模块sample、randint、shuffle、c

    一、random模块简介 Python标准库中的random函数,可以生成随机浮点数、整数、字符串,甚至帮助你随机...

  • 快速计算sin与cos(待修改)

    在嵌入式设备中,浮点数的计算十分缓慢,sin与cos函数的计算更是缓慢。如果想提速,对于sin与cos,可以用查表...

  • JS ------ parseFloat() 函数

    JavaScript parseFloat() 函数将输入的字符串 转换为 浮点数http://www.runoo...

  • #Python 2

    数据类型:字符串str,整数int,浮点数float 字符串拼接:“+”号 相关函数 str() 将其他数据类型...

  • #Python 3

    数据类型:字符串str,整数int,浮点数float 字符串拼接:“+”号 相关函数 str() 将其他数据类型...

网友评论

    本文标题:嵌入式中浮点数转字符串函数

    本文链接:https://www.haomeiwen.com/subject/fbfhdqtx.html