剰余

[具体的な関数]

[使用例]

  1. fmod,remainder,remquo の使用方法です.
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    			// b = 1.5 
    	double s = 3.14, b = 1.5;
    	cout << "***s : " << s << ", b : " << b <<  ", s/b : " << s/b << endl;
    	cout << "  fmod : (s, b) " << fmod(s, b) << " (s, -b) " << fmod(s, -b) << " (-s, b) " << fmod(-s, b) << " (-s, -b) " << fmod(-s, -b) << endl;
    	cout << "  remainder : (s, b) " << remainder(s, b) << " (s, -b) " << remainder(s, -b) << " (-s, b) " << remainder(-s, b) << " (-s, -b) " << remainder(-s, -b) << endl;
    	int q1, q2, q3, q4;
    	double r1 = remquo(s, b, &q1);
    	double r2 = remquo(s, -b, &q2);
    	double r3 = remquo(-s, b, &q3);
    	double r4 = remquo(-s, -b, &q4);
    	cout << "  remquo : (s,b,&q1) (" << r1 << ", " << q1 << ") (s,-b,&q2) (" << r2 << ", " << q2 << ") (-s,b,&q3) (" << r3 << ", " << q3 << ") (-s,-b,&q4) (" << r4 << ", " << q4 << ")" << endl;
    			// b = 1.2 
    	b = 1.2;
    	cout << "***s : " << s << ", b : " << b <<  ", s/b : " << s/b << endl;
    	cout << "  fmod : (s, b) " << fmod(s, b) << " (s, -b) " << fmod(s, -b) << " (-s, b) " << fmod(-s, b) << " (-s, -b) " << fmod(-s, -b) << endl;
    	cout << "  remainder : (s, b) " << remainder(s, b) << " (s, -b) " << remainder(s, -b) << " (-s, b) " << remainder(-s, b) << " (-s, -b) " << remainder(-s, -b) << endl;
    	r1 = remquo(s, b, &q1);
    	r2 = remquo(s, -b, &q2);
    	r3 = remquo(-s, b, &q3);
    	r4 = remquo(-s, -b, &q4);
    	cout << "  remquo : (s,b,&q1) (" << r1 << ", " << q1 << ") (s,-b,&q2) (" << r2 << ", " << q2 << ") (-s,b,&q3) (" << r3 << ", " << q3 << ") (-s,-b,&q4) (" << r4 << ", " << q4 << ")" << endl;
    
    	return 0;
    }
    			
    (出力)
    ***s : 3.14, b : 1.5, s/b : 2.09333
      fmod : (s, b) 0.14 (s, -b) 0.14 (-s, b) -0.14 (-s, -b) -0.14
      remainder : (s, b) 0.14 (s, -b) 0.14 (-s, b) -0.14 (-s, -b) -0.14
      remquo : (s,b,&q1) (0.14, 2) (s,-b,&q2) (0.14, -2) (-s,b,&q3) (-0.14, -2) (-s,-b,&q4) (-0.14, 2)
    ***s : 3.14, b : 1.2, s/b : 2.61667
      fmod : (s, b) 0.74 (s, -b) 0.74 (-s, b) -0.74 (-s, -b) -0.74
      remainder : (s, b) -0.46 (s, -b) -0.46 (-s, b) 0.46 (-s, -b) 0.46
      remquo : (s,b,&q1) (-0.46, 3) (s,-b,&q2) (-0.46, -3) (-s,b,&q3) (0.46, -3) (-s,-b,&q4) (0.46, 3)			

菅沼ホーム 本文目次 演習問題解答例 付録目次 索引