Skip to content

Instantly share code, notes, and snippets.

View simonbyrne's full-sized avatar
🦘

Simon Byrne simonbyrne

🦘
View GitHub Profile
@generated function convertg{T<:Integer}(::Type{Rational{T}}, x::Irrational)
o = precision(BigFloat)
p = 256
while true
setprecision(BigFloat, p)
bx = BigFloat(x())
r = rationalize(T, bx, tol=0)
if abs(BigFloat(r) - bx) > eps(bx)
setprecision(BigFloat, o)
return r
@inline function rotr(x,y)
s = sizeof(x) << 3
(x >> (y % s)) | (x << (-y % s))
end
function foo(state, n)
s = sizeof(state) << 3
t = trailing_zeros(s)-1 # log2(s)-1
p1 = (s>>1 + t)>>1
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
@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
@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
# 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 / IBMFloat64.jl
Created April 23, 2013 14:07
converts 64bit IBM floating point numbers to IEEE754 64bit floats
import Base.convert
bitstype 64 IBMFloat64
# ibm float format is hex based:
# * bit 1 = sign
# * bits 2-8 = exponent to the power of 16, with bias of 64
# * bits 9-64 = mantissa: no implied leading 1 (unlike ieee),
# typically normalised (hex of bits 9-12 is non-zero), though this need
# not be the case
@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
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