aboutsummaryrefslogtreecommitdiff
path: root/semestr-3/anm/numerki/lista1/test.jl
blob: 235800a5b9e81293055f164166f61ad80f789303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Random

function prettyfloat64(f)
    ss = bitstring(f)
    s = ss[1]
    c = ss[2:12]
    m = ss[13:end]
    return string(s, " ", c, " ", m)
end

function string_to_float64(st)
    s = st[1]
    c = st[2:12]
    m = st[13:end]

    if s == '0'
        s = 1.0
    else
        s = -1.0
    end

    c = parse(Int, string("0b", c))
    c = 2.0^(c - 1023)
    m = Float64(parse(Int64, string("0b", m))) / Float64(2^52) + 1.0
    return s * c * m
end

rng = MersenneTwister(123)

function rand_f64_bitstring()
    return randstring(rng, ['0', '1'], 64)
end

function test_string_to_float()
    for i in 1:100000
    # st = rand_f64_bitstring()
        st = rand(rng)
        res = string_to_float64(bitstring(st))
        println(string(st, " -> ", prettyfloat64(st), " -> ", res))
        if bitstring(res) == bitstring(st)
            println("OK")
        else
            println("ZJEBALO SIE")
            break
        end
    end
end

function find_felerny_x()
    while true    
        x = rand(rng) + 1.0
        res = x * (1.0 / x)
        if res != 1.0
            println(string("Znalazlem! ", x))
            return x
        end
    end
end

function find_felerne_iksy(iterations)
    bad_xs = 0
    for i in 1:iterations
        x = rand(rng) + 1.0
        res = x * (1.0 / x)
        if res != 1.0
            bad_xs += 1
        end
    end
    println("Znalazlem ", bad_xs, " felernych iksow, czyli ", bad_xs / iterations * 100.0, "% wszystkich losowanych wartosci.")
end

# find_felerne_iksy(10000000)


function rel_error(x, rx)
    x  = Float64(x)
    rx = Float64(rx)
    relative = abs((x - rx) / x)
    return relative
end

function test_poly(t, poly, x=4.71, exact=-14.636489)
    println("Testing poly \"", poly, "\" with float type ", t, ".")
    res = poly(x, t)
    println(poly, "(", x, ") = ", res)
    println("Relative error (exact: ", exact, "): ", rel_error(exact, res))
end

function w(x, t)
    x = t(x)
    return x^3 - (t(6)) * (x^2) + (t(3)) * x - t(0.149)
end

function w2(x, t)
    x = t(x)
    return ((x - t(6)) * x + t(3)) * x - t(0.149)
end
    
    
function task3()
    println("Task3: ")
    
    test_poly(Float16, w)
    println()
    test_poly(Float32, w)
    println()
    test_poly(Float64, w)
    println()

    test_poly(Float16, w2)
    println()
    test_poly(Float32, w2)
    println()
    test_poly(Float64, w2)
    println()
end