From 5ec5e003b21ac1f06e175898413ada23a6797fc0 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 30 Jul 2015 15:00:38 +1000 Subject: [PATCH] math/fixed: new package. Fixes golang/go#11906 Change-Id: I2b43311ff145e8453cd255f085c82add6da7de5b Reviewed-on: https://go-review.googlesource.com/12863 Reviewed-by: Rob Pike --- math/fixed/fixed.go | 59 ++++++++++++++++++++++++++++++++++++++++ math/fixed/fixed_test.go | 25 +++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 math/fixed/fixed.go create mode 100644 math/fixed/fixed_test.go diff --git a/math/fixed/fixed.go b/math/fixed/fixed.go new file mode 100644 index 0000000..e3dca01 --- /dev/null +++ b/math/fixed/fixed.go @@ -0,0 +1,59 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package fixed implements fixed-point integer types. +package fixed // import "golang.org/x/image/math/fixed" + +import ( + "fmt" +) + +// TODO: implement fmt.Formatter for %f and %g. + +// Int26_6 is a signed 26.6 fixed-point number. +// +// The integer part ranges from -33554432 to 33554431, inclusive. The +// fractional part has 6 bits of precision. +// +// For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4). +type Int26_6 int32 + +// String returns a human-readable representation of a 26.6 fixed-point number. +// +// For example, the number one-and-a-quarter becomes "1:16". +func (x Int26_6) String() string { + const shift, mask = 6, 1<<6 - 1 + if x >= 0 { + return fmt.Sprintf("%d:%02d", int32(x>>shift), int32(x&mask)) + } + x = -x + if x >= 0 { + return fmt.Sprintf("-%d:%02d", int32(x>>shift), int32(x&mask)) + } + return "-33554432:00" // The minimum value is -(1<<25). +} + +// Int52_12 is a signed 52.12 fixed-point number. +// +// The integer part ranges from -2251799813685248 to 2251799813685247, +// inclusive. The fractional part has 12 bits of precision. +// +// For example, the number one-and-a-quarter is Int52_12(1<<12 + 1<<10). +type Int52_12 int64 + +// String returns a human-readable representation of a 52.12 fixed-point +// number. +// +// For example, the number one-and-a-quarter becomes "1:1024". +func (x Int52_12) String() string { + const shift, mask = 12, 1<<12 - 1 + if x >= 0 { + return fmt.Sprintf("%d:%04d", int64(x>>shift), int64(x&mask)) + } + x = -x + if x >= 0 { + return fmt.Sprintf("-%d:%04d", int64(x>>shift), int64(x&mask)) + } + return "-2251799813685248:0000" // The minimum value is -(1<<51). +} diff --git a/math/fixed/fixed_test.go b/math/fixed/fixed_test.go new file mode 100644 index 0000000..e252de7 --- /dev/null +++ b/math/fixed/fixed_test.go @@ -0,0 +1,25 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fixed + +import ( + "testing" +) + +func TestInt26_6(t *testing.T) { + got := Int26_6(1<<6 + 1<<4).String() + want := "1:16" + if got != want { + t.Fatalf("got %q, want %q", got, want) + } +} + +func TestInt52_12(t *testing.T) { + got := Int52_12(1<<12 + 1<<10).String() + want := "1:1024" + if got != want { + t.Fatalf("got %q, want %q", got, want) + } +}