Giter VIP home page Giter VIP logo

Comments (3)

adienes avatar adienes commented on May 12, 2024

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.

nhz2 avatar nhz2 commented on May 12, 2024

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.

uniment avatar uniment commented on May 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.