{"id":22214229,"url":"https://github.com/lordazzi/calc-js","last_synced_at":"2025-07-27T12:32:00.863Z","repository":{"id":19366592,"uuid":"85859991","full_name":"lordazzi/calc-js","owner":"lordazzi","description":"Handle JavaScript operations, avoiding the native problems of the language","archived":false,"fork":false,"pushed_at":"2024-03-31T18:24:18.000Z","size":656,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T08:07:23.281Z","etag":null,"topics":["angular","bigdecimal","calc-js","calculations","floating-point-accuracy-problems","floating-point-arithmetic","ieee-754","ieee-problem","ieee754","javascript","react","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lordazzi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-03-22T18:00:08.000Z","updated_at":"2024-04-12T20:30:39.000Z","dependencies_parsed_at":"2024-01-16T10:36:07.019Z","dependency_job_id":"0bdb009e-e624-451f-95ac-c5164278b7ee","html_url":"https://github.com/lordazzi/calc-js","commit_stats":{"total_commits":100,"total_committers":4,"mean_commits":25.0,"dds":0.6599999999999999,"last_synced_commit":"ad239a3359b240d315ea77dcdf0b30139e674783"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lordazzi%2Fcalc-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lordazzi%2Fcalc-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lordazzi%2Fcalc-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lordazzi%2Fcalc-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lordazzi","download_url":"https://codeload.github.com/lordazzi/calc-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227802816,"owners_count":17822113,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","bigdecimal","calc-js","calculations","floating-point-accuracy-problems","floating-point-arithmetic","ieee-754","ieee-problem","ieee754","javascript","react","typescript"],"created_at":"2024-12-02T21:14:35.628Z","updated_at":"2024-12-02T21:14:36.272Z","avatar_url":"https://github.com/lordazzi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Calc JS\nHandle JavaScript operations, avoiding the native problems of the language.\n\u003e javescripeto le calco\n\n[![npm version](https://badge.fury.io/js/calc-js.svg)](https://badge.fury.io/js/calc-js)\n[![Npm Total Downloads](https://img.shields.io/npm/dt/calc-js.svg)](https://github.com/lordazzi/calc-js)\n[![Npm Monthly Downloads](https://img.shields.io/npm/dm/calc-js.svg)](https://github.com/lordazzi/calc-js)\n[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://github.com/lordazzi/calc-js/blob/documentation/LICENSE)\n[![Package Quality](https://packagequality.com/badge/calc-js.png)](https://packagequality.com/#?package=calc-js)\n\n## The problem\nThe problem of representing decimal numbers encoded in IEEE 754 binary format is a problem that JavaScript suffers.\nThe bug consists of the lack of ability for the format to represent some numerical values in the binary format, you can read an article talking about it here:  [Double precision floating-point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format).\n\nThis is a well-known problem, in Java they have developed a class called BigDecimal to solve this.\n\n![Error example](docs/problem.png)\n\n![Error example](docs/problem2.png)\n\nThe problem is generated when you perform a simple calculation using some decimal values, example:\n```javascript\n0.2 + 0.1 = 0.30000000000000004\n```\n\nBut the same problem does not occur when the same result is reached using powers of 10, example:\n```javascript\n3 / 10 = 0.3\n```\n\n## Instalation\n```sh\nnpm install calc-js --save\n```\n\n```javascript\n//  node\nvar Calc = require('calc-js').Calc;\n```\n\n```typescript\n//  typescript\nimport { Calc } from 'calc-js';\n```\n\n## How to use\nThe use of the library is very simple:\n\n```typescript\nimport { Calc } from 'calc-js';\n\n// 0.2 + 0.1\nconst numericResult = new Calc(0.2).sum(0.1).finish();\n\n// 0.2 - 0.1\nconst numericResult = new Calc(0.2).minus(0.1).finish();\n\n// 2199 / 100 * 5\nconst numericResult = new Calc(2199).divide(100).multiply(5).finish();\n```\n\nThis is an example with TypeScript, but you can also use this in JavaScript application as you wish.\n\n## Error Handling\nThere are some EcmaScript errors that are not thrown by the language and it cannot be treated,\nthese erros are: zero division and infinity numbers `0 / 10 = Infinity`, non numberic math\noperations `'Some text' * 10 = NaN` and insecure numbers (too large number that language cannot\nrepresent, but still allow you to use then).\n\nThe library will generate an error object when these situations are found, this is the CalcError\nand it extends native JavaScript Error. Use this will give you a stack trace to found the problem cause.\n\n![The CalcError](docs/error-object.png)\n\nYou can choose four ways to deal with errors:\n\n### 1. Ignore then\nIgnore all identified errors\n```typescript\nCalc.configure({\n  throwNaN: false,\n  throwInfinite: false,\n  throwUnsafeNumber: false\n});\n```\n\n### 2. Listen an event\nRegister a function on library to listen all thrown errors. This will cover\nall Calc in application so, is a centralized treatment\n\n```typescript\nCalc.configure({\n  // you don't need really configure this, 'emit-event' is the default value\n  thrownStrategy: 'emit-event'\n});\n\nCalc.onError(function(error) {\n  //  do something\n});\n```\nNote: if you choose emit-event and don't associate any function to listen the errors,\nthe application will thrown then\n\n### 3. Throw\nThis is a non centralized solution. You'll use this when the calc is critical\nand should not generate an invalid result.\n\n```typescript\nCalc.configure({\n  thrownStrategy: 'thrown'\n});\n\ntry {\n  const result = new Calc(10).sum(11).finish();\n} catch (e) {\n  //  do something\n}\n```\n\n### 4. Show in console\nThis will write the error in the browser console\n\n```typescript\nCalc.configure({\n  thrownStrategy: 'console'\n});\n```\n\n## Config\nAll library settings are related to how errors will be thrown. The following exemple show\nhow to change these configs and show the default values set in the library:\n\n```typescript\nCalc.configure({\n  thrownStrategy: 'emit-event',\n  throwNaN: true,\n  throwInfinite: true,\n  throwUnsafeNumber: true\n});\n```\n\nThis configure function will override the default values to each Calc execution\nin your application, but these default values can be overriden in each execution\n(configure localy will never required, but optional):\n\n```typescript\nconst localConfig = { thrownStrategy: 'thrown' };\nnew Calc(10, localConfig).divide(0).finish();\n```\n\n```typescript\nconst localConfig = { thrownStrategy: 'thrown' };\n//  the old way to calc in the library\nCalc.divide(10, 0, localConfig);\n```\n\n```typescript\nconst localConfig = { thrownStrategy: 'thrown' };\n//  it will throw if this isn't a valid number\nCalc.checkNumber(Infinity, 'Validating data from query param', localConfig);\n```\n\n## Contributing\n\n### 1. Create an issue\nNo one feature will be implemented without it having an open issue and without which the proposed has been accepted by the team responsible for the project. After the issue is approved, the applicant, a team member or anyone else can open a pull request associated with that issue (just paste the issue link in the pull request).\n\n### 2. Did you find a bug?\nWhen logging a bug, please be sure to include the following:\n * The library version;\n * If at all possible, an *isolated* way to reproduce the behavior;\n * The behavior you expect to see, and the actual behavior.\n\nYou can try to update the library to the last version to see if the bug has already been fixed.\n\n### 3. Do not create a duplicate issue\n[Search the existing issues](https://github.com/lordazzi/calc-js/search?type=Issues) before logging a new one.\n\nSome search tips:\n * *Don't* restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title.\n * Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes.\n\n### 4. Create a Pull Request\nFollow the steps:\n\n * Create a [fork](https://guides.github.com/activities/forking/) from our repository by [clicking here](https://github.com/lordazzi/calc-js/fork), install [node](https://nodejs.org/), do a `git clone` of your forked repository and run `npm install` in the application folder;\n * Create a branch in your forked repository, then code the feature or fix the bug;\n * Run `npm run lint`, `npm run test` and `npm run build` in the repository;\n * Create a Pull Request from your repository to this one, with the issue in the body and some information you think could be usefull to the reviewer (print or a [gif of it working](https://www.screentogif.com/) will be appreciated);\n * The reviewer can ask some changes, don't be mad, this is the process;\n * You get approved and your branch with the feature / fix \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flordazzi%2Fcalc-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flordazzi%2Fcalc-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flordazzi%2Fcalc-js/lists"}