Skip to content

Sum-safe Rounding for Collections in Clojurescript

Notifications You must be signed in to change notification settings

cgdeboer/vectoround

Repository files navigation

Vectoround: Sum-safe Rounding for collections

https://travis-ci.org/cgdeboer/vectoround.svg?branch=master

Leinengen coordinates:

Vectoround is a sum-safe rounding library for collections (lists, vectors, sets, maps).

Example Code:

(ns sample.core
  (:require [vectoround.core :refer [saferound]])

(def pre-rounded-vectormap
  [{:name "foo" :value 60.19012964572332}
   {:name "bar" :value 15.428802458406679}
   {:name "baz" :value 24.381067895870007}])

(saferound pre-rounded-vectormap 0 :value)

;; => [{:name "foo", :value 60.0}
;;     {:name "bar", :value 16.0}
;;     {:name "baz", :value 24.0}]

;; => original sum (100) is preserved.

How It Works

Vectoround provides a function called saferound that takes the following 3 ordered inputs:

a collection (map, vector, list or set): a data structure.
A structure that contains floats to be rounded. e.g a vector of floats, a map with float values, a vector of maps contains floats.
places (int): Places for rounding.
Number of places each item in the set should be rounded to.
field (keyword, or nil):

if the collection is a list/vector of maps, else pass nil

Vectoround uses the 'difference' strategy and seeks to minimize the sum of the array of the differences between the original value and the rounded value of each item in the collection. It will adjust the items with the largest difference to preserve the sum.

will return nil or the same data structure that was passed with the rounded values.

Feature Support

Vectoround definitely supports at least these collections.

  • list (either list of floats, or list of maps)
  • vector (either vector of floats, or vector of maps)
  • map (rounds the values)

Documentation

Documentation beyond this readme not available.

How to Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).
  3. Write a test which shows that the bug was fixed or that the feature works as expected.
  4. Send a pull request. Make sure to add yourself to AUTHORS.

About

Sum-safe Rounding for Collections in Clojurescript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published