03 Types, type conversions and floating point arithmetic
Exercise 03.1
Compare the computed values of
and
when , and . Store in the variable d0
and in the variable d1
.
Try checking for equality, e.g. print(d0 == d1)
.
a = ...
b = ...
c = ...
d0 = ...
print(d0)
d1 = ...
print(d1)
print(d0 == d1)
## tests ##
assert d0 == 30.0
assert d1 != 30.0
assert d0 != d1
练习 03.1
比较以下两个计算值:
和
当 , 和 时。将 (d_{0}) 的值存储在变量 d0
中,将 (d_{1}) 的值存储在变量 d1
中。
尝试检查两者是否相等,例如:print(d0 == d1)
。
当 , 和 时:
首先计算 :
然后计算 :
接下来,我们可以将这两个计算值进行编程比较。
a = 100
b = 0.1
c = 0.2
# 计算d0和d1的值
d0 = a * b + a * c
d1 = a * (b + c)
# 输出d0和d1的值
print("d0 =", d0)
print("d1 =", d1)
# 检查d0和d1是否相等
print("d0 and d1 are equal:", d0 == d1)
运行上述代码,我们可以获得 和 的值,以及它们是否相等的结果。
Exercise 03.2
For the polynomial
compute using: (i) the compact form ; and (ii) the expanded form for:
(a) and
(b) and
and compare the number of significant digits for which the answers are the same.
Store the answer for the compact version using the variable f0
, and using the variable f1
for the expanded version.
For case (b), compare the computed and analytical solutions and consider the relative error.
Which approach would you recommend for computing this expression?
公众号:AI悦创【二维码】

AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
