Skip to content

Instantly share code, notes, and snippets.

View simonbyrne's full-sized avatar
🦘

Simon Byrne simonbyrne

🦘
View GitHub Profile
function setup(pkgname)
Pkg.cd() do
cd(pkgname) do
info("Generating ssh key")
run(`ssh-keygen -N "" -f .quickdocs_id_rsa`)
info("Encrypting private key")
run(`travis login --auto`)
run(`travis encrypt-file .quickdocs_id_rsa --add deploy`)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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 / 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))))
@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