Round a triplet of reals to a proper hex object
this needs to preserve the q+r+s==0 condition
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=8), | intent(in) | :: | xyz(3) |
pure function hex_round(xyz) result(qrs) !! Round a triplet of reals to a proper hex object !! > this needs to preserve the q+r+s==0 condition real(8),intent(in) :: xyz(3) type(hex) :: qrs real(8) :: x,y,z integer :: q,r,s real(8) :: dq,dr,ds ! Unpack x = xyz(1) y = xyz(2) z = xyz(3) ! Round xyz q = nint(x) r = nint(y) s = nint(z) ! Eval diffs dq = abs(x-q) dr = abs(x-r) ds = abs(x-s) ! Reset bigger diff if(dq > dr .and. dq > ds)then q = -r-s elseif(dr > ds)then r = -q-s else s = -q-r endif ! Repack qrs = hex(q,r,s) ! internal assertion :) end function hex_round