log10

package module
v0.0.0-...-f050996 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2021 License: MIT Imports: 1 Imported by: 0

README

Package log10 calculates log base 10 of an integer, fast.

It is inspired by Daniel Lemire's blog post on this topic.

TODO:

Documentation

Overview

Package log10 calculates log base 10 of an integer. This is a simple task. This package focuses on performance, as there are non-obvious techniques for speed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Uint32

func Uint32(x uint32) int

Uint32 returns the number of digits required to hold the base 10 representation of x. As a special case, Uint32(0) == 1.

Note that if you are making a byte buffer for a base 10 representation of x, you will often get better results by calling make with a large-enough constant. Be sure to benchmark both approaches.

Implementation inspired by https://lemire.me/blog/2021/05/28/computing-the-number-of-digits-of-an-integer-quickly/#comment-585476 and tweaked for the Go compiler.

Example
package main

import (
	"fmt"

	"github.com/josharian/log10"
)

func main() {
	examples := []uint32{0, 1, 9, 10, 11}
	for _, e := range examples {
		fmt.Printf("log10.Uint32(%d) = %d\n", e, log10.Uint32(e))
	}
}
Output:

log10.Uint32(0) = 1
log10.Uint32(1) = 1
log10.Uint32(9) = 1
log10.Uint32(10) = 2
log10.Uint32(11) = 2

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL