Skip to content
/ cround Public

A floating point number rounding package for Go which calls the rounding functions from the C standard library.

Notifications You must be signed in to change notification settings

mhale/cround

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

cround

A floating-point number rounding package for Go which calls the rounding functions from the C standard library.

The Go standard library does not provide a rounding function in the math package, which requires developers to write their own. Here are some rounding functions you can use instead of doing that.

The default rounding mode is "banker's rounding". That is, values are rounded to the nearest integer value of x, with ties rounded to even integers.

For a pure Go version of this functionality, see the round package.

Usage

Using this package may require a C compiler to be installed on the system, which is not ideal for all developers. However it does allow Go programs to make use of the special case handling in mature C implementations to theoretically improve accuracy.

Import cround as you would any other package, and then call one of the exposed functions. See the source code for a listing.

If you are not sure which function to use, you should use Round. A more explicit function is ToNearestEven.

Round

Round is a convenience wrapper for ToNearestEven.

value := cround.Round(2.5)
// value is 2.0

RoundTo

RoundTo is a convenience wrapper for ToNearestEven, which rounds to a specified number of decimal places.

value := cround.RoundTo(1234.5678, 2)
// value is 1234.57

ToNearestEven

ToNearestEven rounds to the nearest integer value, with ties rounded to even integers.

value := cround.ToNearestEven(2.5)
// value is 2.0

Miscellaneous

These links may be useful to learn about floating-point rounding modes.

Testing

The tests cover a range of test values, including the zero and infinity special cases. NaN is not included because a comparison between two NaN values always returns false.

Please submit any additional test cases that demonstrate errors or inaccuracies.

Licensing

This code is in the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute the original cround code, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

About

A floating point number rounding package for Go which calls the rounding functions from the C standard library.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages