Skip to content

BigFloat incorrectly rounded to Float16 (subnormals) #50642

Closed
@nsajko

Description

@nsajko

julia_rounding_experiment.jl

distance(x, y) = abs(x - y)

function rounding_experiment(bf::BigFloat, r::AbstractFloat)
  p = prevfloat(r)
  n = nextfloat(r)

  r_dist = distance(bf, r)
  p_dist = distance(bf, p)
  n_dist = distance(bf, n)

  if !((r_dist  p_dist) & (r_dist  n_dist))
    print("rounding $r not correct: ")
    if p_dist  n_dist
      println("$p is the correct rounding")
    else
      println("$n is the correct rounding")
    end
  end

  nothing
end

f16_rounding_experiment(bf::BigFloat) = rounding_experiment(bf, Float16(bf))

setprecision(BigFloat, 500)

f16_rounding_experiment(big"1.4901162082026128889687591176485489397376143775948511e-07")
julia> include("/home/nsajko/julia_rounding_experiment.jl")
rounding 1.0e-7 not correct: 2.0e-7 is the correct rounding

julia> versioninfo()
Julia Version 1.11.0-DEV.139
Commit bf00ff4a110 (2023-07-21 19:34 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 1 on 8 virtual cores

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions