1

float 표현 방식 예제

(a)

1
print(1e3)

1000.0


(b)

1
2
3
4
if 1e3 == 1000:
    print(True)
else:
    print(False)

True


(c)

1
print(1e-03)

0.001


(d)

1
2
3
4
if 1E-3 == 1/1000:
    print(True)
else:
    print(False)

True


(e)

1
2
str = "{:6.4f}".format(-1.408)
print(str)

1.4080


(f)

1
2
str = "{:6.2f}".format(-1.408)
print(str)

-1.41


2

math package의 exp()함수

(a)

1
2
import math
print (math.exp(0))

1.0


(b)

1
2
import math
print(math.exp(1))

2.718281828459045


(c)

1
2
import math
print(math.exp(1)*math.exp(1))

7.3890560989306495


(d)

1
2
import math
print(math.exp(2))

7.38905609893065


(e)

1
2
import math
print(math.exp(2)-math.exp(1)*math.exp(1))

8.881784197001252e-16


3

f() 함수에 대한 질문들

1
2
def f(x):
    return 1/math.exp(x**2)*math.cos(5*x) - 0.1

(a)

1
print(f(0))

0.9


(b)

1
print("{:5.4f}".format(f(-1.408)))

0.0001


(c)

1
print("{:5.4f}".format(f(-1.408176)))

0.0000


(d)

1
2
print("{:5.4f}".format(f(-1.6)))
print("{:5.4f}".format(f(-1.4)))

-0.1112 0.0062


(e)

-1.46 < x < -1.39일때 f(x) == 0 되는 x가 존재하는가?

f(-1.46)는 음수고 (-1.39)sms didtndlek. Ekfktj f(x)는 0이 되는 지점이 이 구간 어딘가에 존재한다. 그래프를 그려보면 확실히 알 수 있음


4

함수를 인자로 받는 함수 crossing_finder()에 대한 질문들

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def crossing_finder(f,start=-10,end=10,n=100):
    r = end - start
    x_list = [i / n * r + start for i in range(n)]
    y_list = [f(x) for x in x_list]
    prev_x = x_list[0]
    prev_y = y_list[0]
    crossing_list = []
    for i in range(1,n):
        x = x_list[i]
        y = y_list[i]
        if prev_y * y < 0:
            crossing_list.append((prev_x,x))
        prev_x = x
        prev_y = y
    return crossing_list

(a)

1
2
3
4
f = lambda x: 1/math.exp(x**2) * math.cos(5*x) - 0.1
c = crossing_finder(f)
for (start,end) in c:
    print("({:6.4f},{:6.4f})".format(start,end))

(-1.6000,-1.4000) (-1.0000,-0.8000) (-0.4000,-0.2000) (0.2000,0.4000) (0.8000,1.0000) (1.4000,1.6000)


(b)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def zero_finder(f,start,end,eps=1E-9):
    x = (start + end)/2
    y1 = f(start)
    y = f(x)
    y2 = f(end)
    if math.fabs(y) < eps:
        return x
    elif y1*y < 0:
        return zero_finder(f,start,x,eps)
    else:
        return zero_finder(f,x,end,eps)
ans_list = []
for (start,end) in c:
    ans_list.append(zero_finder(f,start,end))
for i in range(len(ans_list)):
    ans = ans_list[i]
    print("ans[{1}] = {0:6.4f}".format(ans,i))

ans[0] = -1.4082 ans[1] = -0.9972 ans[2] = -0.2923 ans[3] = 0.2923 ans[4] = 0.9972 ans[5] = 1.4082