julia-color/example.qmd

79 lines
1.3 KiB
Plaintext

---
title: "Some Julia Code"
keep-ipynb: true
keep-tex: true
cache: true
format:
julia-html: default
julia-pdf:
pdf-engine: xelatex
papersize: a4
code-block-bg: "#e0e0e0"
---
## Colored console graphs produced by `Benchmarktools.jl`
```{julia}
using BenchmarkTools
@benchmark sum(rand(1000))
```
## Structure of floating point numbers
```{julia}
function printbitsf64(x::Float64)
s = bitstring(x)
printstyled(s[1], color = :blue, reverse=true)
printstyled(s[2:12], color = :green, reverse=true)
printstyled(s[13:end], color=:red, bold=true, reverse=true)
print("\n")
end
printbitsf64(27.56640625)
```
### Illustrate machine epsilon...
```{julia}
Eps=0.5
while 1 != 1 + Eps
Eps /= 2
printbitsf64(1+Eps)
end
```
### ... some ugly colors
```{julia}
function printbits2f64(x::Float64)
s = bitstring(x)
printstyled(s[1], color = 142, reverse=true)
printstyled(s[2:12], color = 190, reverse=false, underline=true)
printstyled(s[13:end], color= 27, bold=true, reverse=true)
print("\n")
end
printbits2f64(27.56640625)
```
## Errors and Warnings
```{julia}
#| error: true
#| warn: true
println(π^2)
ceil(2.3+5.6im)
```
The `@warn` macro writes to the `stderr` channel:
```{julia}
#| error: true
#| warn: true
println(π^2)
@warn "Last warning!"
1 + 41
```