JuliaRandom / JuliaRandom/RandomNumbers.jl

Conversion to Float

Aperta
#8 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Julia
Stelle
100
Fork
23
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

The "obvious" approach of converting to a float, then multiplying by a scale factor is slow, and has the potential to give an answer of `1.0`.

The easiest option is:

``` julia
import Base: significand_mask, exponent_one
f1(u::UInt64) = reinterpret(Float64, exponent_one(Float64) | significand_mask(Float64) & u) - 1.0
```

unfortunately this has the downside that the last bit will always be zero, so you only get 52 bits of randomness per float: see https://github.com/JuliaLang/julia/issues/16344.

A slightly more advanced option (based on [this proposal](http://stackoverflow.com/a/35351145/392585)) is:

``` julia
import Base: significand_mask, significand_bits, exponent_half
function f2(u::UInt64)
f = reinterpret(Float64, exponent_half(Float64) | 0x001f_ffff_ffff_ffff & u)
if (u >> significand_bits(Float64)) &1 == 1
f-= 0.5
end
f
end
```

This gets us 53 bits, but introduces a branch. There might be some clever stuff we can do here though.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Start with the current float-conversion approach described in the issue and compare the f1 and f2 alternatives, including the linked Julia issue about the lost last bit. The change is complete when conversion avoids producing 1.0 while preserving 53 bits of randomness without an unacceptable performance cost.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
julia
Ambito
performance
Tipo di issue
Refactoring
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.