English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
fmod(x, y) = x - tquote * y
dove tquote viene troncata, ossia il risultato di x / y (arrotondamento).
double fmod(double x, double y); float fmod(float x, float y); long double fmod(long double x, long double y); double fmod(Type1 x, Type2 y); // Caricamento aggiuntivo per combinazioni di tipi aritmetici diversi
La funzione fmod() accetta due parametri e restituisce un valore di tipo double, float o long double. Questa funzione è disponibile<cmath>Definito nei file di intestazione.
x: valore del numeratore.
y: valore del denominatore.
La funzione fmod() restituisce il resto a virgola mobile di x / y. Se il denominatore y è zero, fmod() restituisce NaN (non numerico).
#include <iostream> #include <cmath> using namespace std; int main() { double x = 7.5, y = 2.1; double result = fmod(x, y); cout << "余数 " << x << "/" << y << " = " << result << endl; x = -17.50, y = 2.0; result = fmod(x, y); cout << "余数 " << x << "/" << y << " = " << result << endl; return0; }
输出程序运行时
Il resto 7.5/2.1 = 1.2 Il resto -17.5/2 = -1.5
#include <iostream> #include <cmath> using namespace std; int main() { double x = 12.19, result; int y = -3; result = fmod(x, y); cout << "余数 " << x << "/" << y << " = " << result << endl; y = 0; result = fmod(x, y); cout << "余数 " << x << "/" << y << " = " << result << endl; return0; }
输出程序运行时
余数12.19/-3 = 0.19 余数12.19/0 = -nan