Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juliamath/changeprecision.jl
macro to change the default floating-point precision in Julia code
https://github.com/juliamath/changeprecision.jl
floating-point julia math
Last synced: about 1 month ago
JSON representation
macro to change the default floating-point precision in Julia code
- Host: GitHub
- URL: https://github.com/juliamath/changeprecision.jl
- Owner: JuliaMath
- License: other
- Created: 2017-10-24T20:53:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-01-02T21:57:22.000Z (11 months ago)
- Last Synced: 2024-10-12T23:20:56.696Z (about 1 month ago)
- Topics: floating-point, julia, math
- Language: Julia
- Size: 42 KB
- Stars: 37
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ChangePrecision
[![CI](https://github.com/JuliaMath/ChangePrecision.jl/workflows/CI/badge.svg)](https://github.com/JuliaMath/ChangePrecision.jl/actions?query=workflow%3ACI)
This package makes it easy to change the "default" precision of a large body of Julia code, simply by prefixing it with the `@changeprecision T expression` macro, for example:
```julia
@changeprecision Float32 begin
x = 7.3
y = 1/3
z = rand() .+ ones(3,4)
end
```In particular, floating-point literals like `7.3` are reinterpreted as the requested type `Float32`, operations like `/` that convert integer arguments to `Float64` instead convert to `Float32`, and random-number or matrix constructors like `rand` and `ones` default to `Float32` instead of `Float64`.
Several other cases are handled as well: arithmetic with irrational constants like `pi`, linear-algebra functions (like `inv`) on integer matrices, etcetera.The `@changeprecision` transformations are applied recursively to any `include(filename)` call, so that you can simply do `@changeprecision Float32 include("mycode.jl")` to run a whole script `mycode.jl` in `Float32` default precision.
Code that explicitly specifies a type, e.g. `rand(Float64)`, is unaffected by `@changeprecision`.
Note that only expressions that *explicitly appear* in the `expression` (or code inserted by `include`) are converted by `@changeprecision`. Code *hidden inside* external functions that are called is not affected.
## This package is for quick experiments, not production code
This package was designed for quick hacks, where you want to experiment with the effect of a change in precision on a bunch of code. For production code and long-term software development in Julia, you are strongly urged to write precision-independent code — that is, your functions should determine their working precision from the precision of their *arguments*, so that by simply passing data in a different precision they compute in that precision.