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 5package 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// Modf(±Inf) = ±Inf, NaN 12// Modf(NaN) = NaN, NaN 13func Modf(f float64) (int float64, frac float64) { 14 return modf(f) 15} 16 17func modf(f float64) (int float64, frac float64) { 18 if f < 1 { 19 switch { 20 case f < 0: 21 int, frac = Modf(-f) 22 return -int, -frac 23 case f == 0: 24 return f, f // Return -0, -0 when f == -0 25 } 26 return 0, f 27 } 28 29 x := Float64bits(f) 30 e := uint(x>>shift)&mask - bias 31 32 // Keep the top 12+e bits, the integer part; clear the rest. 33 if e < 64-12 { 34 x &^= 1<<(64-12-e) - 1 35 } 36 int = Float64frombits(x) 37 frac = f - int 38 return 39} 40