跳至主要內容

03 Types, type conversions and floating point arithmetic

AI悦创原创python 1v1留学生作业辅导剑桥大学大约 2 分钟...约 685 字

Exercise 03.1

EN

Compare the computed values of

d0=ab+ac d_0 = a \cdot b + a \cdot c

and

d1=a(b+c) d_1 = a \cdot (b + c)

when a=100a = 100, b=0.1b = 0.1 and c=0.2c = 0.2. Store d0d_{0} in the variable d0 and d1d_{1} 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

Exercise 03.2

For the polynomial

f(x,y)=(x+y)6=x6+6x5y+15x4y2+20x3y3+15x2y4+6xy5+y6 \begin{align} f(x, y) &= (x + y)^{6} \\ &= x^6 + 6x^{5}y + 15x^{4}y^{2} + 20x^{3}y^{3} + 15x^{2}y^{4} + 6xy^{5} + y^{6} \end{align}

compute ff using: (i) the compact form (x+y)6(x + y)^{6}; and (ii) the expanded form for:

(a) x=10x = 10 and y=10.1y = 10.1

(b) x=10x = 10 and y=10.1y = -10.1

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度