├── .gitignore ├── CHANGES.md ├── CReal.ml ├── CReal.mli ├── CReal.opam ├── LICENSE ├── Makefile ├── README.md ├── draw_sin.ml ├── dune ├── dune-project ├── test.expected └── test.ml /.gitignore: -------------------------------------------------------------------------------- 1 | /.merlin 2 | /_build/ 3 | /*.install 4 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | 2 | * fixed bug in `exp` (patch from Nathanaëlle Courant) 3 | * fixed bug in `mul_Bexp` (patch from Nathanaëlle Courant) 4 | 5 | -------------------------------------------------------------------------------- /CReal.ml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/backtracking/creal/0520c6351fdc431db3c0941b5cf111211826f88a/CReal.ml -------------------------------------------------------------------------------- /CReal.mli: -------------------------------------------------------------------------------- 1 | (* 2 | * Exact real arithmetic (Constructive reals). 3 | * Copyright (C) 2000 Jean-Christophe FILLIATRE 4 | * 5 | * This software is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License version 2, as published by the Free Software Foundation. 8 | * 9 | * This software is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the GNU Library General Public License version 2 for more details 14 | * (enclosed in the file LGPL). 15 | *) 16 | 17 | (*s {\bf Constructive reals} are implemented by the following abstract 18 | datatype [t]. If [x] is a constructive real, then the function call 19 | [approx x n] returns an approximation of [x] up to $4^{-n}$, as 20 | an arbitrary precision integer $x_n$ such that $|4^n\cdot x - x_n| < 1$. *) 21 | 22 | type t 23 | 24 | val create' : (int -> t) -> t 25 | 26 | val approx : t -> int -> Z.t 27 | 28 | val msd : t -> int 29 | 30 | (*s Basic operations. *) 31 | 32 | val add : t -> t -> t 33 | val neg : t -> t 34 | val sub : t -> t -> t 35 | 36 | val abs : t -> t 37 | 38 | val mul : t -> t -> t 39 | val inv : t -> t 40 | val div : t -> t -> t 41 | 42 | val pow_int : t -> int -> t 43 | val root : int -> t -> t 44 | 45 | val sqrt : t -> t 46 | 47 | (*s Transcendental functions. [log ~base:x y] is $\log_x(y)$. *) 48 | 49 | val ln : t -> t 50 | val log : base:t -> t -> t 51 | 52 | val exp : t -> t 53 | val pow : t -> t -> t 54 | 55 | (*s Trigonometric functions. *) 56 | 57 | val sin : t -> t 58 | val cos : t -> t 59 | val tan : t -> t 60 | 61 | val arcsin : t -> t 62 | val arccos : t -> t 63 | val arctan : t -> t 64 | 65 | (*s [arctan_reciproqual n] is $\arctan(1/n)$, but is more efficient than 66 | using [arctan]. *) 67 | 68 | val arctan_reciproqual : int -> t 69 | 70 | (*s Hyperbolic functions. *) 71 | 72 | val sinh : t -> t 73 | val cosh : t -> t 74 | val tanh : t -> t 75 | 76 | val arcsinh : t -> t 77 | val arccosh : t -> t 78 | val arctanh : t -> t 79 | 80 | (*s Some constants. *) 81 | 82 | val zero : t 83 | val one : t 84 | val two : t 85 | 86 | val pi : t 87 | val half_pi : t 88 | 89 | val e : t 90 | 91 | (*s Comparisons. [cmp] is absolute comparison: it may not terminate and only 92 | returns [-1] or [+1]. [rel_cmp] is relative comparison, up to $4^{-k}$, 93 | and it returns [-1], [0] or [+1]. *) 94 | 95 | val compare : t -> t -> int 96 | val rel_cmp : int -> t -> t -> int 97 | 98 | val min : t -> t -> t 99 | val max : t -> t -> t 100 | 101 | (*s Coercions. [to_q] and [to_float] expect a precision. [to_float x 102 | n] returns the best floating point representation of the rational 103 | $\ap{x}{n} / 4^n$. [of_string] expects a base as second argument. *) 104 | 105 | val of_int : int -> t 106 | val of_z : Z.t -> t 107 | val of_q : Q.t -> t 108 | val of_float : float -> t 109 | val of_string : ?radix:int -> string -> t 110 | 111 | val to_float : t -> int -> float 112 | val to_q : t -> int -> Q.t 113 | 114 | (*s Coercion to type [string]. Given a decimal precision [p], 115 | [to_string x p] returns a decimal approximation [d] of [x] with 116 | either [p] digits such that $|d - x| < 10^{-p}$, or [p+1] digits 117 | such that $|d - x| < 10^{-p-1}$. 118 | 119 | [to_beautiful_string] returns the same decimal number but with 120 | digits packed 5 by 5. *) 121 | 122 | val to_string : t -> int -> string 123 | val to_beautiful_string : t -> int -> string 124 | 125 | (*s Format pretty-printer. *) 126 | 127 | val print : Format.formatter -> t -> unit 128 | val set_print_precision : int -> unit 129 | 130 | (*s Infix notations. *) 131 | 132 | module Infixes : sig 133 | val ( +! ) : t -> t -> t 134 | val ( -! ) : t -> t -> t 135 | val ( *! ) : t -> t -> t 136 | val ( /! ) : t -> t -> t 137 | end 138 | 139 | -------------------------------------------------------------------------------- /CReal.opam: -------------------------------------------------------------------------------- 1 | opam-version: "2.0" 2 | version: "0.8" 3 | maintainer: "Jean-Christophe.Filliatre@cnrs.fr" 4 | authors: [ 5 | "Jean-Christophe Filliâtre" 6 | "François Bobot" 7 | ] 8 | homepage: "https://github.com/backtracking/creal" 9 | dev-repo: "git://github.com/backtracking/creal" 10 | bug-reports: "https://github.com/backtracking/creal/issues" 11 | license: "LGPL-2.1" 12 | build: [ 13 | [ "dune" "build" "--for-release-of-packages" "CReal" "-j" jobs ] 14 | [ "dune" "runtest" "--for-release-of-packages" "CReal" "-j" jobs] { with-test } 15 | ] 16 | 17 | depends: [ 18 | "dune" {build} 19 | "zarith" 20 | "ocaml" { >= "4.02.3" } 21 | ] 22 | 23 | synopsis: "Library of computational real, allows to compute a real at specified precision. Equality is not terminating" 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The library Creal is distributed under the terms of the GNU Lesser 2 | General Public License version 2.1 (included below). 3 | 4 | As a special exception to the GNU Lesser General Public License, you 5 | may link, statically or dynamically, a "work that uses the Library" 6 | with a publicly distributed version of the Library to produce an 7 | executable file containing portions of the Library, and distribute 8 | that executable file under terms of your choice, without any of the 9 | additional requirements listed in clause 6 of the GNU Lesser General 10 | Public License. By "a publicly distributed version of the Library", we 11 | mean either the unmodified Library as distributed, or a 12 | modified version of the Library that is distributed under the 13 | conditions defined in clause 2 of the GNU Lesser General Public 14 | License. This exception does not however invalidate any other reasons 15 | why the executable file might be covered by the GNU Lesser General 16 | Public License. 17 | 18 | ====================================================================== 19 | 20 | GNU LESSER GENERAL PUBLIC LICENSE 21 | Version 2.1, February 1999 22 | 23 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 24 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 25 | Everyone is permitted to copy and distribute verbatim copies 26 | of this license document, but changing it is not allowed. 27 | 28 | [This is the first released version of the Lesser GPL. It also counts 29 | as the successor of the GNU Library Public License, version 2, hence 30 | the version number 2.1.] 31 | 32 | Preamble 33 | 34 | The licenses for most software are designed to take away your 35 | freedom to share and change it. By contrast, the GNU General Public 36 | Licenses are intended to guarantee your freedom to share and change 37 | free software--to make sure the software is free for all its users. 38 | 39 | This license, the Lesser General Public License, applies to some 40 | specially designated software packages--typically libraries--of the 41 | Free Software Foundation and other authors who decide to use it. You 42 | can use it too, but we suggest you first think carefully about whether 43 | this license or the ordinary General Public License is the better 44 | strategy to use in any particular case, based on the explanations 45 | below. 46 | 47 | When we speak of free software, we are referring to freedom of use, 48 | not price. Our General Public Licenses are designed to make sure that 49 | you have the freedom to distribute copies of free software (and charge 50 | for this service if you wish); that you receive source code or can get 51 | it if you want it; that you can change the software and use pieces of 52 | it in new free programs; and that you are informed that you can do 53 | these things. 54 | 55 | To protect your rights, we need to make restrictions that forbid 56 | distributors to deny you these rights or to ask you to surrender these 57 | rights. These restrictions translate to certain responsibilities for 58 | you if you distribute copies of the library or if you modify it. 59 | 60 | For example, if you distribute copies of the library, whether gratis 61 | or for a fee, you must give the recipients all the rights that we gave 62 | you. You must make sure that they, too, receive or can get the source 63 | code. If you link other code with the library, you must provide 64 | complete object files to the recipients, so that they can relink them 65 | with the library after making changes to the library and recompiling 66 | it. And you must show them these terms so they know their rights. 67 | 68 | We protect your rights with a two-step method: (1) we copyright the 69 | library, and (2) we offer you this license, which gives you legal 70 | permission to copy, distribute and/or modify the library. 71 | 72 | To protect each distributor, we want to make it very clear that 73 | there is no warranty for the free library. Also, if the library is 74 | modified by someone else and passed on, the recipients should know 75 | that what they have is not the original version, so that the original 76 | author's reputation will not be affected by problems that might be 77 | introduced by others. 78 | 79 | Finally, software patents pose a constant threat to the existence of 80 | any free program. We wish to make sure that a company cannot 81 | effectively restrict the users of a free program by obtaining a 82 | restrictive license from a patent holder. Therefore, we insist that 83 | any patent license obtained for a version of the library must be 84 | consistent with the full freedom of use specified in this license. 85 | 86 | Most GNU software, including some libraries, is covered by the 87 | ordinary GNU General Public License. This license, the GNU Lesser 88 | General Public License, applies to certain designated libraries, and 89 | is quite different from the ordinary General Public License. We use 90 | this license for certain libraries in order to permit linking those 91 | libraries into non-free programs. 92 | 93 | When a program is linked with a library, whether statically or using 94 | a shared library, the combination of the two is legally speaking a 95 | combined work, a derivative of the original library. The ordinary 96 | General Public License therefore permits such linking only if the 97 | entire combination fits its criteria of freedom. The Lesser General 98 | Public License permits more lax criteria for linking other code with 99 | the library. 100 | 101 | We call this license the "Lesser" General Public License because it 102 | does Less to protect the user's freedom than the ordinary General 103 | Public License. It also provides other free software developers Less 104 | of an advantage over competing non-free programs. These disadvantages 105 | are the reason we use the ordinary General Public License for many 106 | libraries. However, the Lesser license provides advantages in certain 107 | special circumstances. 108 | 109 | For example, on rare occasions, there may be a special need to 110 | encourage the widest possible use of a certain library, so that it 111 | becomes a de-facto standard. To achieve this, non-free programs must 112 | be allowed to use the library. A more frequent case is that a free 113 | library does the same job as widely used non-free libraries. In this 114 | case, there is little to gain by limiting the free library to free 115 | software only, so we use the Lesser General Public License. 116 | 117 | In other cases, permission to use a particular library in non-free 118 | programs enables a greater number of people to use a large body of 119 | free software. For example, permission to use the GNU C Library in 120 | non-free programs enables many more people to use the whole GNU 121 | operating system, as well as its variant, the GNU/Linux operating 122 | system. 123 | 124 | Although the Lesser General Public License is Less protective of the 125 | users' freedom, it does ensure that the user of a program that is 126 | linked with the Library has the freedom and the wherewithal to run 127 | that program using a modified version of the Library. 128 | 129 | The precise terms and conditions for copying, distribution and 130 | modification follow. Pay close attention to the difference between a 131 | "work based on the library" and a "work that uses the library". The 132 | former contains code derived from the library, whereas the latter must 133 | be combined with the library in order to run. 134 | 135 | GNU LESSER GENERAL PUBLIC LICENSE 136 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 137 | 138 | 0. This License Agreement applies to any software library or other 139 | program which contains a notice placed by the copyright holder or 140 | other authorized party saying it may be distributed under the terms of 141 | this Lesser General Public License (also called "this License"). 142 | Each licensee is addressed as "you". 143 | 144 | A "library" means a collection of software functions and/or data 145 | prepared so as to be conveniently linked with application programs 146 | (which use some of those functions and data) to form executables. 147 | 148 | The "Library", below, refers to any such software library or work 149 | which has been distributed under these terms. A "work based on the 150 | Library" means either the Library or any derivative work under 151 | copyright law: that is to say, a work containing the Library or a 152 | portion of it, either verbatim or with modifications and/or translated 153 | straightforwardly into another language. (Hereinafter, translation is 154 | included without limitation in the term "modification".) 155 | 156 | "Source code" for a work means the preferred form of the work for 157 | making modifications to it. For a library, complete source code means 158 | all the source code for all modules it contains, plus any associated 159 | interface definition files, plus the scripts used to control 160 | compilation and installation of the library. 161 | 162 | Activities other than copying, distribution and modification are not 163 | covered by this License; they are outside its scope. The act of 164 | running a program using the Library is not restricted, and output from 165 | such a program is covered only if its contents constitute a work based 166 | on the Library (independent of the use of the Library in a tool for 167 | writing it). Whether that is true depends on what the Library does 168 | and what the program that uses the Library does. 169 | 170 | 1. You may copy and distribute verbatim copies of the Library's 171 | complete source code as you receive it, in any medium, provided that 172 | you conspicuously and appropriately publish on each copy an 173 | appropriate copyright notice and disclaimer of warranty; keep intact 174 | all the notices that refer to this License and to the absence of any 175 | warranty; and distribute a copy of this License along with the 176 | Library. 177 | 178 | You may charge a fee for the physical act of transferring a copy, 179 | and you may at your option offer warranty protection in exchange for a 180 | fee. 181 | 182 | 2. You may modify your copy or copies of the Library or any portion 183 | of it, thus forming a work based on the Library, and copy and 184 | distribute such modifications or work under the terms of Section 1 185 | above, provided that you also meet all of these conditions: 186 | 187 | a) The modified work must itself be a software library. 188 | 189 | b) You must cause the files modified to carry prominent notices 190 | stating that you changed the files and the date of any change. 191 | 192 | c) You must cause the whole of the work to be licensed at no 193 | charge to all third parties under the terms of this License. 194 | 195 | d) If a facility in the modified Library refers to a function or a 196 | table of data to be supplied by an application program that uses 197 | the facility, other than as an argument passed when the facility 198 | is invoked, then you must make a good faith effort to ensure that, 199 | in the event an application does not supply such function or 200 | table, the facility still operates, and performs whatever part of 201 | its purpose remains meaningful. 202 | 203 | (For example, a function in a library to compute square roots has 204 | a purpose that is entirely well-defined independent of the 205 | application. Therefore, Subsection 2d requires that any 206 | application-supplied function or table used by this function must 207 | be optional: if the application does not supply it, the square 208 | root function must still compute square roots.) 209 | 210 | These requirements apply to the modified work as a whole. If 211 | identifiable sections of that work are not derived from the Library, 212 | and can be reasonably considered independent and separate works in 213 | themselves, then this License, and its terms, do not apply to those 214 | sections when you distribute them as separate works. But when you 215 | distribute the same sections as part of a whole which is a work based 216 | on the Library, the distribution of the whole must be on the terms of 217 | this License, whose permissions for other licensees extend to the 218 | entire whole, and thus to each and every part regardless of who wrote 219 | it. 220 | 221 | Thus, it is not the intent of this section to claim rights or contest 222 | your rights to work written entirely by you; rather, the intent is to 223 | exercise the right to control the distribution of derivative or 224 | collective works based on the Library. 225 | 226 | In addition, mere aggregation of another work not based on the Library 227 | with the Library (or with a work based on the Library) on a volume of 228 | a storage or distribution medium does not bring the other work under 229 | the scope of this License. 230 | 231 | 3. You may opt to apply the terms of the ordinary GNU General Public 232 | License instead of this License to a given copy of the Library. To do 233 | this, you must alter all the notices that refer to this License, so 234 | that they refer to the ordinary GNU General Public License, version 2, 235 | instead of to this License. (If a newer version than version 2 of the 236 | ordinary GNU General Public License has appeared, then you can specify 237 | that version instead if you wish.) Do not make any other change in 238 | these notices. 239 | 240 | Once this change is made in a given copy, it is irreversible for 241 | that copy, so the ordinary GNU General Public License applies to all 242 | subsequent copies and derivative works made from that copy. 243 | 244 | This option is useful when you wish to copy part of the code of 245 | the Library into a program that is not a library. 246 | 247 | 4. You may copy and distribute the Library (or a portion or 248 | derivative of it, under Section 2) in object code or executable form 249 | under the terms of Sections 1 and 2 above provided that you accompany 250 | it with the complete corresponding machine-readable source code, which 251 | must be distributed under the terms of Sections 1 and 2 above on a 252 | medium customarily used for software interchange. 253 | 254 | If distribution of object code is made by offering access to copy 255 | from a designated place, then offering equivalent access to copy the 256 | source code from the same place satisfies the requirement to 257 | distribute the source code, even though third parties are not 258 | compelled to copy the source along with the object code. 259 | 260 | 5. A program that contains no derivative of any portion of the 261 | Library, but is designed to work with the Library by being compiled or 262 | linked with it, is called a "work that uses the Library". Such a 263 | work, in isolation, is not a derivative work of the Library, and 264 | therefore falls outside the scope of this License. 265 | 266 | However, linking a "work that uses the Library" with the Library 267 | creates an executable that is a derivative of the Library (because it 268 | contains portions of the Library), rather than a "work that uses the 269 | library". The executable is therefore covered by this License. 270 | Section 6 states terms for distribution of such executables. 271 | 272 | When a "work that uses the Library" uses material from a header file 273 | that is part of the Library, the object code for the work may be a 274 | derivative work of the Library even though the source code is not. 275 | Whether this is true is especially significant if the work can be 276 | linked without the Library, or if the work is itself a library. The 277 | threshold for this to be true is not precisely defined by law. 278 | 279 | If such an object file uses only numerical parameters, data 280 | structure layouts and accessors, and small macros and small inline 281 | functions (ten lines or less in length), then the use of the object 282 | file is unrestricted, regardless of whether it is legally a derivative 283 | work. (Executables containing this object code plus portions of the 284 | Library will still fall under Section 6.) 285 | 286 | Otherwise, if the work is a derivative of the Library, you may 287 | distribute the object code for the work under the terms of Section 6. 288 | Any executables containing that work also fall under Section 6, 289 | whether or not they are linked directly with the Library itself. 290 | 291 | 6. As an exception to the Sections above, you may also combine or 292 | link a "work that uses the Library" with the Library to produce a 293 | work containing portions of the Library, and distribute that work 294 | under terms of your choice, provided that the terms permit 295 | modification of the work for the customer's own use and reverse 296 | engineering for debugging such modifications. 297 | 298 | You must give prominent notice with each copy of the work that the 299 | Library is used in it and that the Library and its use are covered by 300 | this License. You must supply a copy of this License. If the work 301 | during execution displays copyright notices, you must include the 302 | copyright notice for the Library among them, as well as a reference 303 | directing the user to the copy of this License. Also, you must do one 304 | of these things: 305 | 306 | a) Accompany the work with the complete corresponding 307 | machine-readable source code for the Library including whatever 308 | changes were used in the work (which must be distributed under 309 | Sections 1 and 2 above); and, if the work is an executable linked 310 | with the Library, with the complete machine-readable "work that 311 | uses the Library", as object code and/or source code, so that the 312 | user can modify the Library and then relink to produce a modified 313 | executable containing the modified Library. (It is understood 314 | that the user who changes the contents of definitions files in the 315 | Library will not necessarily be able to recompile the application 316 | to use the modified definitions.) 317 | 318 | b) Use a suitable shared library mechanism for linking with the 319 | Library. A suitable mechanism is one that (1) uses at run time a 320 | copy of the library already present on the user's computer system, 321 | rather than copying library functions into the executable, and (2) 322 | will operate properly with a modified version of the library, if 323 | the user installs one, as long as the modified version is 324 | interface-compatible with the version that the work was made with. 325 | 326 | c) Accompany the work with a written offer, valid for at least 327 | three years, to give the same user the materials specified in 328 | Subsection 6a, above, for a charge no more than the cost of 329 | performing this distribution. 330 | 331 | d) If distribution of the work is made by offering access to copy 332 | from a designated place, offer equivalent access to copy the above 333 | specified materials from the same place. 334 | 335 | e) Verify that the user has already received a copy of these 336 | materials or that you have already sent this user a copy. 337 | 338 | For an executable, the required form of the "work that uses the 339 | Library" must include any data and utility programs needed for 340 | reproducing the executable from it. However, as a special exception, 341 | the materials to be distributed need not include anything that is 342 | normally distributed (in either source or binary form) with the major 343 | components (compiler, kernel, and so on) of the operating system on 344 | which the executable runs, unless that component itself accompanies 345 | the executable. 346 | 347 | It may happen that this requirement contradicts the license 348 | restrictions of other proprietary libraries that do not normally 349 | accompany the operating system. Such a contradiction means you cannot 350 | use both them and the Library together in an executable that you 351 | distribute. 352 | 353 | 7. You may place library facilities that are a work based on the 354 | Library side-by-side in a single library together with other library 355 | facilities not covered by this License, and distribute such a combined 356 | library, provided that the separate distribution of the work based on 357 | the Library and of the other library facilities is otherwise 358 | permitted, and provided that you do these two things: 359 | 360 | a) Accompany the combined library with a copy of the same work 361 | based on the Library, uncombined with any other library 362 | facilities. This must be distributed under the terms of the 363 | Sections above. 364 | 365 | b) Give prominent notice with the combined library of the fact 366 | that part of it is a work based on the Library, and explaining 367 | where to find the accompanying uncombined form of the same work. 368 | 369 | 8. You may not copy, modify, sublicense, link with, or distribute 370 | the Library except as expressly provided under this License. Any 371 | attempt otherwise to copy, modify, sublicense, link with, or 372 | distribute the Library is void, and will automatically terminate your 373 | rights under this License. However, parties who have received copies, 374 | or rights, from you under this License will not have their licenses 375 | terminated so long as such parties remain in full compliance. 376 | 377 | 9. You are not required to accept this License, since you have not 378 | signed it. However, nothing else grants you permission to modify or 379 | distribute the Library or its derivative works. These actions are 380 | prohibited by law if you do not accept this License. Therefore, by 381 | modifying or distributing the Library (or any work based on the 382 | Library), you indicate your acceptance of this License to do so, and 383 | all its terms and conditions for copying, distributing or modifying 384 | the Library or works based on it. 385 | 386 | 10. Each time you redistribute the Library (or any work based on the 387 | Library), the recipient automatically receives a license from the 388 | original licensor to copy, distribute, link with or modify the Library 389 | subject to these terms and conditions. You may not impose any further 390 | restrictions on the recipients' exercise of the rights granted herein. 391 | You are not responsible for enforcing compliance by third parties with 392 | this License. 393 | 394 | 11. If, as a consequence of a court judgment or allegation of patent 395 | infringement or for any other reason (not limited to patent issues), 396 | conditions are imposed on you (whether by court order, agreement or 397 | otherwise) that contradict the conditions of this License, they do not 398 | excuse you from the conditions of this License. If you cannot 399 | distribute so as to satisfy simultaneously your obligations under this 400 | License and any other pertinent obligations, then as a consequence you 401 | may not distribute the Library at all. For example, if a patent 402 | license would not permit royalty-free redistribution of the Library by 403 | all those who receive copies directly or indirectly through you, then 404 | the only way you could satisfy both it and this License would be to 405 | refrain entirely from distribution of the Library. 406 | 407 | If any portion of this section is held invalid or unenforceable under 408 | any particular circumstance, the balance of the section is intended to 409 | apply, and the section as a whole is intended to apply in other 410 | circumstances. 411 | 412 | It is not the purpose of this section to induce you to infringe any 413 | patents or other property right claims or to contest validity of any 414 | such claims; this section has the sole purpose of protecting the 415 | integrity of the free software distribution system which is 416 | implemented by public license practices. Many people have made 417 | generous contributions to the wide range of software distributed 418 | through that system in reliance on consistent application of that 419 | system; it is up to the author/donor to decide if he or she is willing 420 | to distribute software through any other system and a licensee cannot 421 | impose that choice. 422 | 423 | This section is intended to make thoroughly clear what is believed to 424 | be a consequence of the rest of this License. 425 | 426 | 12. If the distribution and/or use of the Library is restricted in 427 | certain countries either by patents or by copyrighted interfaces, the 428 | original copyright holder who places the Library under this License 429 | may add an explicit geographical distribution limitation excluding those 430 | countries, so that distribution is permitted only in or among 431 | countries not thus excluded. In such case, this License incorporates 432 | the limitation as if written in the body of this License. 433 | 434 | 13. The Free Software Foundation may publish revised and/or new 435 | versions of the Lesser General Public License from time to time. 436 | Such new versions will be similar in spirit to the present version, 437 | but may differ in detail to address new problems or concerns. 438 | 439 | Each version is given a distinguishing version number. If the Library 440 | specifies a version number of this License which applies to it and 441 | "any later version", you have the option of following the terms and 442 | conditions either of that version or of any later version published by 443 | the Free Software Foundation. If the Library does not specify a 444 | license version number, you may choose any version ever published by 445 | the Free Software Foundation. 446 | 447 | 14. If you wish to incorporate parts of the Library into other free 448 | programs whose distribution conditions are incompatible with these, 449 | write to the author to ask for permission. For software which is 450 | copyrighted by the Free Software Foundation, write to the Free 451 | Software Foundation; we sometimes make exceptions for this. Our 452 | decision will be guided by the two goals of preserving the free status 453 | of all derivatives of our free software and of promoting the sharing 454 | and reuse of software generally. 455 | 456 | NO WARRANTY 457 | 458 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 459 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 460 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 461 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 462 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 463 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 464 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 465 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 466 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 467 | 468 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 469 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 470 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 471 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 472 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 473 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 474 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 475 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 476 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 477 | DAMAGES. 478 | 479 | END OF TERMS AND CONDITIONS 480 | 481 | How to Apply These Terms to Your New Libraries 482 | 483 | If you develop a new library, and you want it to be of the greatest 484 | possible use to the public, we recommend making it free software that 485 | everyone can redistribute and change. You can do so by permitting 486 | redistribution under these terms (or, alternatively, under the terms 487 | of the ordinary General Public License). 488 | 489 | To apply these terms, attach the following notices to the library. 490 | It is safest to attach them to the start of each source file to most 491 | effectively convey the exclusion of warranty; and each file should 492 | have at least the "copyright" line and a pointer to where the full 493 | notice is found. 494 | 495 | 496 | 497 | Copyright (C) 498 | 499 | This library is free software; you can redistribute it and/or 500 | modify it under the terms of the GNU Lesser General Public 501 | License as published by the Free Software Foundation; either 502 | version 2.1 of the License, or (at your option) any later version. 503 | 504 | This library is distributed in the hope that it will be useful, 505 | but WITHOUT ANY WARRANTY; without even the implied warranty of 506 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 507 | Lesser General Public License for more details. 508 | 509 | You should have received a copy of the GNU Lesser General Public 510 | License along with this library; if not, write to the Free Software 511 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 512 | 513 | Also add information on how to contact you by electronic and paper mail. 514 | 515 | You should also get your employer (if you work as a programmer) or 516 | your school, if any, to sign a "copyright disclaimer" for the library, 517 | if necessary. Here is a sample; alter the names: 518 | 519 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 520 | library `Frob' (a library for tweaking knobs) written by James 521 | Random Hacker. 522 | 523 | , 1 April 1990 524 | Ty Coon, President of Vice 525 | 526 | That's all there is to it! 527 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | dune build @all 4 | 5 | .PHONY: all test 6 | 7 | test: 8 | dune runtest 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Creal - an exact real arithmetic library for OCaml 2 | 3 | This is a straightforward implementation of Valérie Ménissier-Morain's Phd 4 | See https://theses.fr/1994PA077271 (in French) 5 | -------------------------------------------------------------------------------- /draw_sin.ml: -------------------------------------------------------------------------------- 1 | 2 | (* Drawing the sin function accurately using exact real arithmetic *) 3 | 4 | open Format 5 | open Graphics 6 | open CReal 7 | 8 | let xmin = of_string Sys.argv.(1) 9 | let xmax = of_string Sys.argv.(2) 10 | let () = printf "%a..%a@." print xmin print xmax 11 | 12 | let width = 800 13 | let height = 600 14 | let () = open_graph (sprintf " %dx%d" width height) 15 | 16 | let ymin = of_string "-1.0" 17 | let ymax = of_string "1.0" 18 | 19 | let px = 2. /. float height 20 | let prec = truncate (ceil (-. Stdlib.log px /. Stdlib.log 4.)) 21 | let () = printf "prec = %d@." prec 22 | 23 | let xstep = div (sub xmax xmin) (of_int width) 24 | let stepy = div (of_int height) (sub ymax ymin) 25 | 26 | let sin gx = 27 | let x = add xmin (mul (of_int gx) xstep) in 28 | let y = sin x in 29 | let gy = mul (sub y ymin) stepy in 30 | truncate (to_float gy prec) 31 | 32 | let () = 33 | moveto 0 (sin 0); 34 | for i = 1 to width - 1 do 35 | lineto i (sin i) 36 | done 37 | 38 | 39 | 40 | let () = ignore (read_key ()) 41 | -------------------------------------------------------------------------------- /dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name CReal) 3 | (public_name CReal) 4 | (modules CReal) 5 | (libraries zarith) 6 | ) 7 | 8 | (test 9 | (name test) 10 | (modules Test) 11 | (libraries CReal) 12 | ) 13 | 14 | (executable 15 | (name draw_sin) 16 | (modules draw_sin) 17 | (libraries graphics CReal)) 18 | -------------------------------------------------------------------------------- /dune-project: -------------------------------------------------------------------------------- 1 | (lang dune 1.1) 2 | -------------------------------------------------------------------------------- /test.expected: -------------------------------------------------------------------------------- 1 | *** Sanity checks *** 2 | 3 | sqrt(2)^2 = 2... ok 4 | 5 | 1/sqrt(2) = sqrt(2)/2... ok 6 | 7 | 1 = (sqrt(3) + sqrt(2)) * (sqrt(3) - sqrt(2))... ok 8 | 9 | (sqrt(2) ^ sqrt(2)) ^ sqrt(2) = 2... ok 10 | 11 | 54^1/3 - 2^1/3 = 16^1/3... ok 12 | 13 | cos(0)=1... ok 14 | 15 | cos(pi/2)=0... ok 16 | 17 | sin(0)=0... ok 18 | 19 | sin(pi/2)=1... ok 20 | 21 | cos^2(pi/4) + sin^2(pi/4) = 1... ok 22 | 23 | tan(pi/4) = 1... ok 24 | 25 | pi/4 = 4arctan(1/5) - arctan(1/239)... ok 26 | 27 | ln(1) = 0... ok 28 | 29 | ln(e) = 1... ok 30 | 31 | ln(pi*pi) = 2ln(pi)... ok 32 | 33 | exp(-pi) = exp(-pi/2) * exp(-pi/2)... ok 34 | 35 | 36 | *** Benchmarks *** 37 | 38 | golden ratio = 1.61803 39887 49894 84820 45868 34365 63811 77203 09179 80576 39 | 40 | e = 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69996 41 | 42 | pi = 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37511 43 | 44 | sqrt(pi) = 1.77245385090551602729816748334114518279754945612239 45 | 46 | sin(e) = 0.41078129050290869547600949201836059188830697039342 47 | 48 | cos(e) = -0.91173391478696509789371731780543184525041342921570 49 | 50 | sin(sin(sin(1))) = 0.67843047736074022897916012648038632505211726009610 51 | 52 | cos(cos(cos(1))) = 0.654289790497779149970966471327808496626615495472595 53 | 54 | exp(exp(exp(1))) = 3814279.10476022059220921959409820357102394053622666607553 55 | 56 | ln(pi) = 1.14472988584940017414342735135305871164729481291531 57 | 58 | ln(1+ln(1+ln(1+pi))) = 0.63351070055219619318532156988033216142740343241327 59 | 60 | ln(1+ln(1+ln(1+e))) = 0.60903623754620029367154766695069282835040492671909 61 | 62 | sin(1e50) = -0.78967249342931008271028953991740775396008340462140 63 | 64 | cos(1e50) = -0.61352860823366356226485295130376280010719043970532 65 | 66 | pi^2/6 = 1.64493 40668 48226 43647 24151 66646 02518 92189 49901 20680 67 | 68 | -------------------------------------------------------------------------------- /test.ml: -------------------------------------------------------------------------------- 1 | 2 | (*s Test program for [Creal]. *) 3 | 4 | open Printf 5 | open CReal 6 | open CReal.Infixes 7 | 8 | (*s Options *) 9 | 10 | let prec = ref 50 11 | let display = ref true 12 | let sanity_check = ref false 13 | let exit_on_error = ref false 14 | 15 | let _ = 16 | Arg.parse 17 | ["-p", Arg.Int ((:=) prec), "n set the precision"; 18 | "-silent", Arg.Clear display, " no display"; 19 | "-check", Arg.Set sanity_check, " only sanity checks"; 20 | "-exit-on-error", Arg.Set exit_on_error, " exit with error code 1 if an error occurs" 21 | ] 22 | (fun s -> raise (Arg.Bad ("unknown option " ^ s))) 23 | "test [-p prec] [silent]" 24 | 25 | (*s Sanity checks. Compare two numbers up to the precision. *) 26 | 27 | let _ = 28 | if true then begin 29 | printf "*** Sanity checks ***\n\n"; flush stdout 30 | end 31 | 32 | let check msg x y = 33 | if true then begin 34 | printf "%s... " msg; flush stdout; 35 | let delta = Z.sub (approx x !prec) (approx y !prec) in 36 | if Z.compare (Z.abs delta) Z.one <= 0 then 37 | printf "ok\n\n" 38 | else begin 39 | printf "FAILED!\n\n"; if !exit_on_error then exit 1 40 | end; 41 | flush stdout 42 | end 43 | 44 | let sqrt_2 = sqrt two 45 | 46 | let _ = check "sqrt(2)^2 = 2" (sqrt_2 *! sqrt_2) two 47 | 48 | let _ = check "1/sqrt(2) = sqrt(2)/2" (inv sqrt_2) (sqrt_2 /! two) 49 | 50 | let sqrt_3 = sqrt (of_int 3) 51 | 52 | let _ = check "1 = (sqrt(3) + sqrt(2)) * (sqrt(3) - sqrt(2))" 53 | one ((sqrt_3 +! sqrt_2) *! (sqrt_3 -! sqrt_2)) 54 | 55 | let _ = check "(sqrt(2) ^ sqrt(2)) ^ sqrt(2) = 2" 56 | (pow (pow sqrt_2 sqrt_2) sqrt_2) two 57 | 58 | let one_third = of_int 1 /! of_int 3 59 | let root3 x = pow x one_third 60 | 61 | let _ = check "54^1/3 - 2^1/3 = 16^1/3" 62 | (root3 (of_int 54) -! root3 two) (root3 (of_int 16)) 63 | 64 | let _ = check "cos(0)=1" (cos zero) one 65 | let _ = check "cos(pi/2)=0" (cos half_pi) zero 66 | let _ = check "sin(0)=0" (sin zero) zero 67 | let _ = check "sin(pi/2)=1" (sin half_pi) one 68 | 69 | let pi_over_4 = pi /! (of_int 4) 70 | let square x = x *! x 71 | let _ = check "cos^2(pi/4) + sin^2(pi/4) = 1" 72 | (square (cos pi_over_4) +! square (sin pi_over_4)) one 73 | 74 | let _ = check "tan(pi/4) = 1" (tan pi_over_4) one 75 | 76 | let _ = check "pi/4 = 4arctan(1/5) - arctan(1/239)" pi_over_4 77 | (of_int 4 *! arctan_reciproqual 5 -! arctan_reciproqual 239) 78 | 79 | let _ = check "ln(1) = 0" (ln one) zero 80 | 81 | let _ = check "ln(e) = 1" (ln e) one 82 | 83 | let _ = check "ln(pi*pi) = 2ln(pi)" (ln (square pi)) (two *! ln pi) 84 | 85 | let _ = check "exp(-pi) = exp(-pi/2) * exp(-pi/2)" 86 | (exp (neg pi)) (let y = exp (neg half_pi) in y *! y) 87 | 88 | let _ = if !sanity_check then exit 0 89 | 90 | 91 | (*s Benchmark. *) 92 | 93 | (* Test function: display the real number, if not [silent] ; otherwise, 94 | just compute the approximation (for timings). *) 95 | 96 | let _ = printf "\n*** Benchmarks ***\n\n"; flush stdout 97 | 98 | let test msg beautiful x = 99 | if !display then begin 100 | printf "%s = " msg; flush stdout; 101 | printf "%s\n\n" 102 | (if beautiful then to_beautiful_string x !prec else to_string x !prec); 103 | flush stdout 104 | end else begin 105 | printf "%s\n" msg; 106 | flush stdout; 107 | ignore (approx x !prec) 108 | end 109 | 110 | (*s golden ratio *) 111 | let phi = (one +! sqrt (of_int 5)) /! (of_int 2) 112 | let _ = test "golden ratio" true phi 113 | 114 | (* e (predefined in [Creal]) *) 115 | let _ = test "e" true e 116 | 117 | (* pi (predefined in [Creal]) *) 118 | let _ = test "pi" true pi 119 | 120 | (*s The Exact Arithmetic Competition: Level 0 Tests 121 | http://www.cs.man.ac.uk/arch/dlester/arithmetic/level0t.html *) 122 | 123 | (* sqrt(pi) *) 124 | let _ = test "sqrt(pi)" false (sqrt pi) 125 | 126 | (* sin(exp(1)) *) 127 | let _ = test "sin(e)" false (sin e) 128 | 129 | (* cos(exp(1)) *) 130 | let _ = test "cos(e)" false (cos e) 131 | 132 | (* sin(sin(sin(1))) *) 133 | let x = sin (sin (sin one)) 134 | let _ = test "sin(sin(sin(1)))" false x 135 | 136 | (* cos(cos(cos(1))) *) 137 | let x = cos (cos (cos one)) 138 | let _ = test "cos(cos(cos(1)))" false x 139 | 140 | (* exp(exp(exp(1))) *) 141 | let x = exp (exp (exp one)) 142 | let _ = test "exp(exp(exp(1)))" false x 143 | 144 | (* log(pi) *) 145 | let _ = test "ln(pi)" false (ln pi) 146 | 147 | (* log(1+log(1+log(1+pi))) *) 148 | let ln_ln_ln_pi = ln (one +! ln (one +! ln (one +! pi))) 149 | let _ = test "ln(1+ln(1+ln(1+pi)))" false ln_ln_ln_pi 150 | 151 | (* log(1+log(1+log(1+exp(1)))) *) 152 | let ln_ln_ln_e = ln (one +! ln (one +! ln (one +! e))) 153 | let _ = test "ln(1+ln(1+ln(1+e)))" false ln_ln_ln_e 154 | 155 | (*i 156 | (* log(1+log(1+log(1+log(1+log(1+log(1+pi)))))) *) 157 | let x = ln (one +! ln (one +! ln (one +! ln_ln_ln_pi))) 158 | let _ = test "ln(1+ln(1+ln(1+ln(1+ln(1+ln(1+pi))))))" false x 159 | 160 | (* log(1+log(1+log(1+log(1+log(1+log(1+exp(1))))))) *) 161 | let x = ln (one +! ln (one +! ln (one +! ln_ln_ln_e))) 162 | let _ = test "ln(1+ln(1+ln(1+ln(1+ln(1+ln(1+e))))))" false x 163 | i*) 164 | 165 | (* sin(1e50) *) 166 | let ten_to_50 = pow_int (of_int 10) 50 167 | let x = sin ten_to_50 168 | let _ = test "sin(1e50)" false x 169 | 170 | (* cos(1e50) *) 171 | let x = cos ten_to_50 172 | let _ = test "cos(1e50)" false x 173 | 174 | let x = div (mul pi pi) (of_int 6) 175 | let _ = test "pi^2/6" true x 176 | 177 | (* arctan(1) *) 178 | (*let _ = test "arctan(1)" false (arctan one)*) 179 | 180 | (*i 181 | 182 | (* BUG GMP 2 *) 183 | let q = 184 | Q.from_zs (Z.from_int 1) (Z.from_string "19807040628566084398385987584" 10) 185 | let _ = Q.add q (Q.from_ints 1 2) 186 | 187 | (* BUG GMP 3 *) 188 | let q = Q.from_zs (Z.from_string "112803124130337404998606757686274889113032882986303222429756948481" 10) (Z.from_string "5192296858534827628530496329220096" 10) 189 | let q' = Q.add q (Q.from_ints 1 2) 190 | let _ = Z.fdiv_q (Q.get_num q') (Q.get_den q') 191 | 192 | let time f x = 193 | let old = Sys.time () in 194 | let y = f x in 195 | Printf.printf "%f\n" (Sys.time () -. old); 196 | y 197 | ;; 198 | 199 | i*) 200 | --------------------------------------------------------------------------------