打印结果时强制小数位数 [英] Enforcing the number of decimal places when printing results
问题描述
另一项运动。这个要求对x-2 = ln(x)的x值进行评估。有两种方法(A和B)-一种使用给定的方程式并得出较小的解(x1)。另一种方法使用e **(x-2)= x并产生另一个解(x2)。
A different exercise. This one asked to evaluate the value of x for x-2=ln(x). There are two approaches (A and B) - one makes use of the given equation and yields the smaller solution (x1). The other approach uses e**(x-2)=x and yields the other solution (x2).
程序绘制图形化解,然后查询初始值输入。然后,它使用适当的方法评估x。方法A要求初始条件小于x2,方法B要求初始条件大于x1。两种方法都适用于介于x1和x2之间的初始条件。
Program plots the graphical solution and then queries for initial value input. Then it evaluates x using appropriate approaches. Approach A requires initial condition lesser than x2, approach B requires initial condition greater than x1. Both approaches work for initial conditions that lay somewhere between x1 and x2.
代码的最后一部分操纵输出以仅打印唯一的解决方案。
The last part of the code manipulates the output to print only unique solutions.
# imports necessary modules
import matplotlib.pyplot as plt
import numpy as np
# plots the equation to provide insight into possible solutions
p = []
x = np.arange(0,5,0.01)
f = x - 2
g = np.log(x)
plt.plot(x,f)
plt.plot(x,g)
plt.show()
# x - 2 = ln(x)
print
lista = map(float, raw_input("Provide the starting conditions to establish the approximate value of the solutions to x-2=ln(x) (separate with spacebar): ").split(" "))
print
sigdig = int(raw_input("Define the number of significant digits (up to 15): "))
print
results1 = []
results2 = []
results3 = []
results4 = []
results = []
resu = []
for i in lista:
if i > 0.1586:
y = i
left = y - 2
right = np.log(y)
expo = "%d" % sigdig
epsi = 10**(-int(expo)-1)
step = 0
while abs(left - right) > epsi:
y = right + 2
right = np.log(y)
left = y - 2
step += 1
results1.append(y)
results2.append(results1[-1])
if i < 3.1462:
z = i
left = np.e ** (z - 2)
right = z
expo = "%d" % sigdig
epsi = 10**(-int(expo)-1)
step = 0
while abs(left - right) > epsi:
z = np.e ** (right - 2)
left = np.e ** (z - 2)
right = z
step += 1
results3.append(z)
results4.append(results3[-1])
# combines and evaluates the results
results = results2 + results4
for i in range(len(results)):
if round(results[i], sigdig) not in resu:
resu.append(round(results[i], sigdig))
else:
pass
print "For given starting conditions following solutions were found:"
print
for i in range(len(resu)):
printer = '"x_%d = %.' + str(sigdig) + 'f" % (i+1, resu[i])'
print eval(printer)
我的问题是:是否可以输入x1和x2的近似值(图形解决方案中的第36和53行)而不是对其进行硬编码?没有代码中的评估方法,是否可以强制执行小数位数?打印结果通常会产生结果,该结果在最接近的十进制 0之前(在代码末尾附近)结束。谢谢。
My questions are: is it possible to feed the approximate values of x1 and x2 (lines 36 and 53) from the graphical solution instead of hard coding them? Is possible to enforce the number of decimals without the eval work-around present in the code? Printing resu[i] normally yields results that end before nearest decimal "0" (near the end of code). Thanks.
推荐答案
您可以在python格式的字符串中使用 *
与C / C ++类似的方式。当%
运算符在格式字符串中遇到 *
时,它将在列表中查找一个整数,并将其用作
You can use *
in python format strings in a similar fashion to C/C++. When the %
operator encounters a *
in the format string, it looks for an integar in the list and uses it as if it were a constant in the format string.
以下内容适用于您要执行的操作:
The following should work for what you are trying to do:
print "x_%d = %.*f" % (sigdig,sigdig,resu[i])
如果结果
为 1.23456
和 sigdig
是 3
,这将输出:
If result
is 1.23456
and sigdig
is 3
, this will output:
x_3 = 1.234
请注意,python float是 IEEE794双精度值,这意味着它们具有大约15个有效数字。例如:
Note that python floats are IEEE794 double precision values, which means they have about 15 significant digits. For example:
>>> print "x_%d = %.*f" % (30,30,1.0/3.0)
x_30 = 0.333333333333333314829616256247
请注意,除第17个小数点以外的所有内容本质上都是随机垃圾。
Note how everything beyond the 17th decimal is essentially random garbage.
这篇关于打印结果时强制小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!