Skip to content

Instantly share code, notes, and snippets.

View simonbyrne's full-sized avatar
🦘

Simon Byrne simonbyrne

🦘
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
const HI = prevfloat(log10(prevfloat(Inf)))
function test(n)
xx = 0.0
zz = 0.0
for i = 1:n
x = reinterpret(Float64, rand(reinterpret(UInt64, 1e-18):reinterpret(UInt64, HI)))
if rand(Bool)
x = -x
end
@simonbyrne
simonbyrne / Make.user
Created March 2, 2017 20:26
Julia Makefile for Raspbian
# sudo apt install llvm-3.9-dev libopenblas-dev libfftw3-dev libgmp3-dev libmpfr-dev libarpack2-dev libopenspecfun-dev libssh2-1-dev libcurl4-openssl-dev
USE_SYSTEM_LLVM:=1
LLVM_CONFIG:=llvm-config-3.9
USE_SYSTEM_BLAS:=1
LIBBLAS:=-lopenblas
LIBBLASNAME:=libopenblas
USE_SYSTEM_LAPACK:=1
USE_SYSTEM_LIBM:=1
@simonbyrne
simonbyrne / paynehanek.jl
Last active November 18, 2023 02:39
Payne-Hanek reduction in Julia
import Base: TwicePrecision, significand_bits, significand_mask, exponent_mask, exponent_bias
# Bits of 1/2π
# 1/2π == sum(x / 0x1p64^i for i,x = enumerate(INV2PI))
# Can be obtained by:
#
# setprecision(BigFloat, 4096)
# I = 0.5/big(pi)
# for i = 1:19
# I *= 0x1p64
@simonbyrne
simonbyrne / ccallmacro.jl
Created January 26, 2017 09:57
A julia ccall macro
@eval macro $(Symbol("ccall"))(expr)
expr.head == :(::) && expr.args[1].head == :call || error("Invalid use of @ccall")
rettype = expr.args[2]
fname = expr.args[1].args[1]
cargs = expr.args[1].args[2:end]
arglist = []
typlist = []
tupexpr = :(())
ccexpr = :(ccall($(esc(fname)), $(esc(rettype)), $(esc(tupexpr))))
# read from Raspbery Pi Sense HAT joystick
function _stick_input_dev()
try
for devname in readdir("/sys/class/input")
sysfname = joinpath("/sys/class/input",devname,"device","name")
if startswith(devname, "event") && isfile(sysfname)
if startswith(readstring(sysfname),"Raspberry Pi Sense HAT Joystick")
return joinpath("/dev/input",devname)
end
@simonbyrne
simonbyrne / spooky.jl
Last active November 1, 2016 01:18
For my Raspberry Pi powered jack-o'-lantern
using SenseHat
import SenseHat: RGB565
function spooky(decay, arriv, refresh)
x = rand()
while true
x *= exp(-decay*refresh)
if rand() < arriv*refresh
x += (1-x)*rand()
end
@simonbyrne
simonbyrne / lerptest.jl
Last active October 5, 2016 15:54
Test of linear interpolation between floating point numbers
function lerp0(j, d, a, b)
linspace(a,b,d+1)[j+1]
end
function lerp1(j, d, a, b)
s = (d-j)/d; t = j/d
a*s + b*t
end
function lerp2(j, d, a, b)
(a*(d-j) + b*j)/d
end
using Unums
import Base: promote_rule
promote_rule{U<:Unums.Ubound}(::Type{U},::Type{Float64}) = U
# Constants
const solar_mass = 4 * pi * pi
const days_per_year = 365.24