8赞
248
当前位置:  开发笔记 > 编程语言 > 正文

从浮点数到整数-Fromafloattoaninteger

Giventhiscodethatmyprofessorgaveusinanexamwhichmeanswecannotmodifythecodenorusef

Given this code that my professor gave us in an exam which means we cannot modify the code nor use function from other libraries (except stdio.h):

鉴于我的教授在考试中给我们的代码,这意味着我们不能修改代码,也不能使用其他库中的函数(stdio.h除外):

float x;

(suppose x NOT having an integer part)
while (CONDITION){
    x = x*10
}

I have to find the condition that makes sure that x has no valid number to the right of decimal point not giving attention to the problems of precision of a float number (After the decimal point we have to have only zeros). I tried this condition:

我必须找到一个条件,确保x在小数点右边没有有效数字,不注意浮点数的精度问题(在小数点之后我们必须只有零)。我试过这个条件:

while ((fmod((x*10),10))){
    X = X*10
}
printf(" %f ",x);

example:

例:

INPUT x=0.456; --------> OUTPUT: 456.000
INPUT X=0.4567;--------> OUTPUT; 4567.000
It is important to be sure that after the decimal point we don't have any        
significant number

But I had to include math.h library BUT my professor doesn't allow us to use it in this specific case (I'm not even allowed to use (long) since we never seen it in class).

但我必须包括math.h库但是我的教授不允许我们在这个特定情况下使用它(我甚至不允许使用(长),因为我们从未在课堂上看过它)。

So what is the condition that solve the problem properly without this library?

那么没有这个库可以正确解决问题的条件是什么?

4 个解决方案

#1


0  

There is fuzziness ("not giving attention to the problems of precision of a float number") in the question, yet I think a sought answer is below, assign x to an integer type until x no longer has a fractional part.

在问题中存在模糊性(“不注意浮点数的精度问题”),但我认为下面是一个寻求的答案,将x指定为整数类型,直到x不再具有小数部分。

Success of this method depends on INT_MIN <= x <= INT_MAX. This is expected when the number of bits in the significant of float does not exceed the value bits of int. Although this is common, it is not specified by C. As an alternative, code could with a wider integer type like long long with a far less chance of the range restriction issue.

此方法的成功取决于INT_MIN <= x <= INT_MAX。当float的有效位中的位数不超过int的值位时,这是预期的。虽然这很常见,但它没有由C指定。作为替代方案,代码可以使用更长的整数类型,例如long long,并且范围限制问题的可能性要小得多。

Given the rounding introduced with *10, this method is not a good foundation of float to text conversion.

鉴于* 10引入了舍入,这种方法不是浮点到文本转换的良好基础。

float Dipok(float x) {
  int i;
  while ((i=x) != x) {
      x = x*10;
  }
  return x;
}

#include 
#include 
#include 

void Dipok_test(float x) {
  // suppose x NOT having an integer part
  assert(x > -1.0 && x <1.0);
  float y = Dipok(x);
  printf("x:%.*f y:%.f\n", FLT_DECIMAL_DIG, x, y);
}

int main(void) {
  Dipok_test(0.456);
  Dipok_test(0.4567);
  return 0;
}

Output

产量

x:0.456000000 y:456
x:0.456699997 y:4567

#2


2  

As pointed out here previously:Due to the accuracy of floats this is not really possible but I think your Prof wants to get something like

正如前面所指出的:由于浮子的准确性,这实际上是不可能的,但我认为你的教授希望得到类似的东西

while (x  - (int)x != 0 )

or

要么

while (x  - (int)x >= 0.00000001 )

You can get rid of the zeroes by using the g modifier instead of f:

您可以使用g修饰符而不是f来消除零:

printf(" %g \n",x);

#3


0  

As already pointed out by 2501, this is just not possible.

正如2501所指出的那样,这是不可能的。

Floats are not accurate. Depending on your platform, the float value for 0.001 is represented as something like 0.0010000001 in fact.

浮点数不准确。根据您的平台,0.001的浮点值实际上表示为0.0010000001。

What would you expect the code to calculate: 10000001 or 1?

你期望代码计算什么:10000001或1?

Any solution will work for some values only.

任何解决方案仅适用于某些值。

#4


0  

I try to answer to my exam question please if I say something wrong correct me!

如果我说错了,我会尽量回答我的考试问题!

It is not possible to find a proper condition that makes sure that there are no valid number after the decimal point. For example : We want to know the result of 0.4*20 which is 8.000 BUT due to imprecision problems the output will be different:

无法找到确保小数点后面没有有效数字的正确条件。例如:我们想知道0.4 * 20的结果是8.000 BUT由于不精确的问题输出会有所不同:

f=0.4;
for(i=1;i<20;i++)
   f=f+0.4;
printf("The number f=0.4*20 is ");
if(f!=8.0) {printf(" not ");}
printf(" %f ",8.0);
printf("The real answer is f=0.4*20= %f",f);

Our OUTPUT will be:

我们的输出将是:

The number f=0.4*20 is not 8.000000 

The real answer is f=0.4*20= 8.000001

推荐阅读
author-avatar
mobiledu2502901317
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有