Math Problem Statement

4.已知f(n)=1111…111B(n+1个1),计算f(n)的C语言函数f1如下: int f1(unsigned n) { int sum = 1, power = 1; for (unsigned i = 0; i <= n - 1; i++) { power *= 2; sum += power; } return sum; }

将f1中的int都改为 float,可得到计算f(n)的另一个函数f2。

float f2(unsigned n) { float sum = 1, power = 1; for (unsigned i = 0; i <= n - 1; i++) { power *= 2; sum += power; } return sum; } 假设unsigned和int型数据都占32位,float采用IEEE754单精度标准,请回答如下问题: (1)当n = 0时,f1会出现死循环,为什么?若将f1中的变量i和n都定义为int型,则f1是否还会出现死循环?为什么? (2)若使f2(n)的结果不溢出,则最大的n是多少?若使f2(n)的结果精确(无舍入),则最大的n是多少?

Solution

Ask a new question for Free

By Image

Drop file here or Click Here to upload

Math Problem Analysis

Mathematical Concepts

Binary numbers
Floating point arithmetic
IEEE 754 standard

Formulas

-

Theorems

-

Suitable Grade Level

Advanced High School