C/C++自己实现remainder函数

简介: C/C++自己实现remainder函数

VS2010不提供remainder这个函数,自己实现,参考官网http://www.cplusplus.com/reference/cmath/remainder/的原理。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
//VS2010不提供remainder这个函数,自己实现
//http://www.cplusplus.com/reference/cmath/remainder/
//Returns the floating - point remainder of numer / denom(rounded to nearest) :
//remainder = numer - rquot * denom
//Where rquot is the result of : numer / denom, rounded toward the nearest integral value(with halfway cases rounded toward the even number).
//A similar function, fmod, returns the same but with the quotient truncated(rounded towards zero) instead.
#define ROUND(d)  int(d + 0.5)//四舍五入
double remainder(double numer, double denom)
{
  int rquot = ROUND(numer / denom);//remainder和fmod的区别就是要不要四舍五入
  double remainder = numer - rquot * denom;
  return remainder;
}
int main()
{
  double da1 = fabs(fmod(5.f, 2.55f));
  double da2 = fabs(remainder(5.f, 2.55f));
  std::cout << "Hello World!\n" << da1  << "," << da2;
}



相关文章
|
4天前
|
C++
C++ 数学函数、头文件及布尔类型详解
C++ 支持数学操作,如`max`和`min`函数找最大值和最小值,以及`&lt;cmath&gt;`库中的`sqrt`、`round`等数学函数。`bool`类型用于布尔逻辑,取值`true`(1)或`false`(0)。布尔表达式结合比较运算符常用于条件判断,例如在`if`语句中检查年龄是否达到投票年龄。在代码示例中,`isCodingFun`和`isFishTasty`变量分别输出1和0。
125 1
|
4天前
|
算法 C++ 容器
C++中模板函数以及类模板的示例(template)
C++中模板函数以及类模板的示例(template)
|
4天前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
|
4天前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
36 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
4天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
15 1
|
4天前
|
存储 算法 数据安全/隐私保护
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
7 0
|
4天前
|
存储 自然语言处理 C++
刷题用到的非常有用的函数c++(持续更新)
刷题用到的非常有用的函数c++(持续更新)
19 1
|
4天前
|
存储 编译器 C++
【C++】内存管理和模板基础(new、delete、类及函数模板)
【C++】内存管理和模板基础(new、delete、类及函数模板)
25 1
|
4天前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)
|
4天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高