https://github.com/cgdeboer/vectoround
Sum-safe Rounding for Collections in Clojurescript
https://github.com/cgdeboer/vectoround
clojurescript rounding
Last synced: 8 months ago
JSON representation
Sum-safe Rounding for Collections in Clojurescript
- Host: GitHub
- URL: https://github.com/cgdeboer/vectoround
- Owner: cgdeboer
- Created: 2019-02-16T18:47:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-22T04:13:08.000Z (over 7 years ago)
- Last Synced: 2025-10-21T23:48:25.360Z (8 months ago)
- Topics: clojurescript, rounding
- Language: Clojure
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
Vectoround: Sum-safe Rounding for collections
=============================================
.. image:: https://travis-ci.org/cgdeboer/vectoround.svg?branch=master
:target: https://travis-ci.org/cgdeboer/vectoround
Leinengen coordinates:
.. image:: https://img.shields.io/clojars/v/vectoround.svg
:target: https://clojars.org/vectoround
Vectoround is a sum-safe rounding library for
collections (lists, vectors, sets, maps).
Example Code:
.. code-block:: clojure
(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 :code:`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 :code:`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 :code:`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
-----------------
#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
#. Fork `the repository`_ on GitHub to start making your changes to the **master** branch (or branch off of it).
#. Write a test which shows that the bug was fixed or that the feature works as expected.
#. Send a pull request. Make sure to add yourself to AUTHORS_.
.. _`the repository`: https://github.com/cgdeboer/vectoround
.. _AUTHORS: https://github.com/cgdeboer/vectoround/blob/master/AUTHORS.rst