Comments (3)
this got me thinking it might be equivalently useful and more general to instead implement round(::T, ::StepRangeLen)
, e.g.
round(0.2, 0:1//4:1)
to return 1//4
from julia.
Something close to your example, apart from floating point rounding is:
julia> floor.(0:1/6:1; digits=1, base=3)
7-element Vector{Float64}:
0.0
0.0
0.3333333333333333
0.3333333333333333
0.6666666666666666
0.6666666666666666
1.0
from julia.
equivalently useful and more general to instead implement
round(::T, ::StepRangeLen)
I prefer the argument order reversed tbh, but that could be useful too. I wouldn't say it's more general, since the range is bounded whereas for the OP the range is unbounded, but it could be useful in its own right for performing a composite round
and clamp
operation while also permitting an offset from zero for the points on the lattice that the rounding operation will snap to.
For example:
round(1//4, 1.3) == 5//4 # this proposal: round to nearest 1//4, unbounded
round(0:1//4:1, 1.3) == 1//1 # StepRangeLen: round to nearest 1//4, clamped to the range 0:1
round(1//4:1//2:9//4, 1.3) == 5//4 # round to nearest 1//2 on a grid offset by 1//4
Something close to your example, apart from floating point rounding
Clever!
from julia.
Related Issues (20)
- Dead link in documentation HOT 3
- `@doc` changes behaviour during testing HOT 3
- frequent segfault in threads tests in GC
- Rename @scriptdir to @script, and look for environments in parent directories
- interpolate variables within markdown admonitions HOT 4
- Broken backward compatibility with Juliacall HOT 4
- REPL history navigation should not mutate history by default HOT 3
- nightly: Cannot load Pkg and REPL in module on LOAD_PATH if REPLExt is in no environment HOT 1
- Unreachable reached at 0x... HOT 2
- Don't box captured variables that are not assigned after closure declaration HOT 6
- Add a function to get indices from a RegexMatch HOT 4
- slow subtyping of complex Union HOT 4
- The behavior of type parameter constraint inheritance is not documented HOT 1
- `Base.require` leads to ERROR: LoadError: UndefVarError: `UUID` not defined in `Main` HOT 3
- Colors are gone in logging on master HOT 2
- Objects captured by finalizer thunks are finalized early. HOT 27
- Thread pool of main thread varies depending on how Julia is started HOT 3
- llvm-julia-16.0.6-2.tar.gz checksum fail HOT 1
- dot function wrong result HOT 4
- mapreduce with multiple arrays allocates HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.