// Copyright 2009-2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package math

/*
	Floating-point mod function.
*/

// Mod returns the floating-point remainder of x/y.
// The magnitude of the result is less than y and its
// sign agrees with that of x.
//
// Special cases are:
//
//	Mod(±Inf, y) = NaN
//	Mod(NaN, y) = NaN
//	Mod(x, 0) = NaN
//	Mod(x, ±Inf) = x
//	Mod(x, NaN) = NaN
func (,  float64) float64 {
	if haveArchMod {
		return archMod(, )
	}
	return mod(, )
}

func mod(,  float64) float64 {
	if  == 0 || IsInf(, 0) || IsNaN() || IsNaN() {
		return NaN()
	}
	 = Abs()

	,  := Frexp()
	 := 
	if  < 0 {
		 = -
	}

	for  >=  {
		,  := Frexp()
		if  <  {
			 =  - 1
		}
		 =  - Ldexp(, -)
	}
	if  < 0 {
		 = -
	}
	return 
}