Skip to content

Instantly share code, notes, and snippets.

View shawnsmithdev's full-sized avatar

Shawn Smith shawnsmithdev

  • Seattle Area
View GitHub Profile
@shawnsmithdev
shawnsmithdev / float32debug.go
Last active August 29, 2015 14:03
Breakdown of float32 (IEEE 754 single-precision binary floating-point format)
package main
import (
"fmt"
"math"
)
const float32MantissaMask = 0x7FFFFF
const float32ExponentMask = 0x7F800000
const float32SignMask = 0x80000000
const float32ExponentBitOffset = 23
// Prints:
// 1 2 3 4 5
// reverse
// 5 4 3 2 1
package main
import (
"fmt"
)
@shawnsmithdev
shawnsmithdev / farey.go
Last active August 29, 2015 14:09
Finds two integers whose ratio is closest to the given floating point value
// farey rational approximation
package main
import (
"fmt"
"math"
)
// x is value to find rational approximation of
@shawnsmithdev
shawnsmithdev / float64debug.go
Last active August 29, 2015 14:15
Breakdown of float64 (IEEE 754 double-precision binary floating-point format)
package main
import (
"fmt"
"math"
)
const float64MantissaMask = 0xFFFFFFFFFFFFF
const float64ExponentMask = 0x7FF0000000000000
const float64SignMask = 0x8000000000000000
@shawnsmithdev
shawnsmithdev / gray-code.go
Last active August 29, 2015 14:15
reflect and prefix algorithm for gray codes
package main
import (
"fmt"
)
func main() {
gray(8)
}
package main
import "fmt"
func roundUpToPow2(x uint64) uint64 {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
@shawnsmithdev
shawnsmithdev / rwmutex.go
Last active October 11, 2015 10:47
Effect of write lock attempt on read lock attempts
package main
import (
"fmt"
"sync"
"time"
)
type info struct {
step, thread uint64
@shawnsmithdev
shawnsmithdev / factdigits.go
Last active November 23, 2015 17:03
Base-10 digits in factorial
package main
import (
"fmt"
"math"
)
// http://www.johndcook.com/blog/2015/10/06/number-of-digits-in-n/
func digitsInFactorial(x int) int {
if x <= 0 {
@shawnsmithdev
shawnsmithdev / nand.go
Created February 25, 2016 11:11
nand logic
import (
"fmt"
)
func nand(x, y bool) bool { return !(x && y) }
func not(x bool) bool { return nand(x, x) }
func and(x, y bool) bool { return not(nand(x, y)) }
func or(x, y bool) bool { return nand(not(x), not(y)) }
func nor(x, y bool) bool { return not(or(x, y)) }
func xor(x, y bool) bool { return nand(nand(x, nand(x, y)), nand(nand(x, y), y)) }
@shawnsmithdev
shawnsmithdev / simpsons.go
Created May 24, 2016 20:00
Numerical Integration
package main
import (
"fmt"
"math"
)
// Simpson's Rule for numerical integration of f from a to b using n steps
// Port of python code: http://stackoverflow.com/questions/16001157/simpsons-rule-in-python
func simpsons(f func(float64) float64, a, b float64, n int) float64 {