github.com/primecitizens/pcz/std@v0.2.1/math/modf.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package math 6 7 // Modf returns integer and fractional floating-point numbers 8 // that sum to f. Both values have the same sign as f. 9 // 10 // Special cases are: 11 // 12 // Modf(±Inf) = ±Inf, NaN 13 // Modf(NaN) = NaN, NaN 14 func Modf(f float64) (int float64, frac float64) { 15 return modf(f) 16 } 17 18 func modf(f float64) (int float64, frac float64) { 19 if f < 1 { 20 switch { 21 case f < 0: 22 int, frac = Modf(-f) 23 return -int, -frac 24 case f == 0: 25 return f, f // Return -0, -0 when f == -0 26 } 27 return 0, f 28 } 29 30 x := Float64bits(f) 31 e := uint(x>>shift)&mask - bias 32 33 // Keep the top 12+e bits, the integer part; clear the rest. 34 if e < 64-12 { 35 x &^= 1<<(64-12-e) - 1 36 } 37 int = Float64frombits(x) 38 frac = f - int 39 return 40 }