{"id":15165911,"url":"https://github.com/stla/lazynumbers","last_synced_at":"2025-10-25T09:31:01.424Z","repository":{"id":62904676,"uuid":"563319360","full_name":"stla/lazyNumbers","owner":"stla","description":"Exact floating-point arithmetic in R.","archived":false,"fork":false,"pushed_at":"2024-01-12T14:12:34.000Z","size":240,"stargazers_count":8,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-31T06:51:17.678Z","etag":null,"topics":["exact-arithmetic","r","rcpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stla.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-11-08T11:18:45.000Z","updated_at":"2024-05-15T16:41:44.000Z","dependencies_parsed_at":"2024-01-12T19:03:54.341Z","dependency_job_id":null,"html_url":"https://github.com/stla/lazyNumbers","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2FlazyNumbers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2FlazyNumbers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2FlazyNumbers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2FlazyNumbers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stla","download_url":"https://codeload.github.com/stla/lazyNumbers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238112937,"owners_count":19418535,"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":["exact-arithmetic","r","rcpp"],"created_at":"2024-09-27T04:06:10.896Z","updated_at":"2025-10-25T09:30:56.135Z","avatar_url":"https://github.com/stla.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: \"lazyNumbers: exact floating-point arithmetic\"\noutput: github_document\n---\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/stla/lazyNumbers/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/stla/lazyNumbers/actions/workflows/R-CMD-check.yaml)\n[![R-CMD-check-valgrind](https://github.com/stla/lazyNumbers/actions/workflows/R-CMD-check-valgrind.yaml/badge.svg)](https://github.com/stla/lazyNumbers/actions/workflows/R-CMD-check-valgrind.yaml)\n\u003c!-- badges: end --\u003e\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(collapse = TRUE)\n```\n\nIt is well-known that floating-point arithmetic is inexact even with some \nsimple operations. For example:\n\n```{r}\n1 - 7*0.1 == 0.3\n```\n\nThis package provides the *lazy numbers*, which allow exact floating-point \narithmetic. These numbers do *not* solve the above issue:\n\n```{r}\nlibrary(lazyNumbers)\nx \u003c- lazynb(1) - lazynb(7)*lazynb(0.1)\nas.double(x) == 0.3\n```\n\nActually one can equivalent define `x` by, shorter, `1 - lazynb(7)*0.1`.\n\nThe above equality does not hold true because `0.1` and `0.3` as double numbers \ndo not exactly represent the true numbers `0.1` and `0.3`:\n\n```{r}\nprint(0.3, digits = 17L)\n```\n\nWhole numbers are exactly represented. The following equality is true:\n\n```{r}\nx \u003c- 1 - lazynb(7)/10\nas.double(x) == 0.3\n```\n\nIt is also possible to compare lazy numbers between them:\n\n```{r}\ny \u003c- lazynb(3)/lazynb(10)\nx == y\n```\n\nAnd one can get a thin interval containing the exact value:\n\n```{r}\nprint(intervals(x), digits = 17L)\n```\n\nHere is a more concrete example illustrating the benefits of the lazy numbers. \nConsider the following recursive sequence:\n\n```{r}\nu \u003c- function(n) {\n  if(n == 1) {\n    return(1/7)\n  }\n  8 * u(n-1) - 1\n}\n```\n\nIt is clear that all terms of this sequence equal `1/7` (approx. `0.1428571`). \nHowever this sequence becomes crazy as `n` increases:\n\n```{r}\nu(15)\nu(18)\nu(20)\nu(30)\n```\n\nThis is not the case of its lazy version:\n\n```{r}\nu \u003c- function(n) {\n  if(n == 1) {\n    return(1/lazynb(7))\n  }\n  8 * u(n-1) - 1\n}\nas.double(u(30))\n```\n\nVectors of lazy numbers and matrices of lazy numbers are implemented. It is \npossible to get the determinant and the inverse of a square lazy matrix:\n\n```{r}\nset.seed(314159L)\n# non-lazy:\nM \u003c- matrix(rnorm(9L), nrow = 3L, ncol = 3L)\ninvM \u003c- solve(M)\nM %*% invM == diag(3L)\n# lazy:\nM_lazy \u003c- lazymat(M)\ninvM_lazy \u003c- lazyInv(M_lazy)\nas.double(M_lazy %*% invM_lazy) == diag(3L)\n```\n\n### About laziness\n\nThe lazy numbers are called like this because when an operation is performed \nbetween numbers, the resulting lazy number is not the result of the operation; \nrather, it is the unevaluated operation. Therefore, performing some operations \non lazy numbers is fast, but a call to `as.double`, which triggers the exact \nevaluation, can be slow. A call to `intervals` is fast.\n\n\n## Blog posts\n\n[The lazy numbers in R](https://laustep.github.io/stlahblog/posts/lazyNumbers.html).\n\n[The lazy numbers in R: correction](https://laustep.github.io/stlahblog/posts/lazyNumbers2.html).\n\n\n## License\n\nThis package is provided under the GPL-3 license but it uses the C++ library \nCGAL. If you wish to use CGAL for commercial purposes, you must obtain a \nlicense from the [GeometryFactory](https://geometryfactory.com).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstla%2Flazynumbers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstla%2Flazynumbers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstla%2Flazynumbers/lists"}