两个浮点数的相加、相减、相除……

<?php

$a = 0.1;
$b = 0.7;
var_dump(bcadd($a,$b,2) == 0.8);

  bcadd — 将两个高精度数字相加

  bccomp — 比较两个高精度数字,返回-1, 0, 1

  bcdiv — 将两个高精度数字相除

  bcmod — 求高精度数字余数

  bcmul — 将两个高精度数字相乘

  bcpow — 求高精度数字乘方

  bcpowmod — 求高精度数字乘方求模,数论里非常常用

  bcscale — 配置默认小数点位数,相当于就是Linux bc中的”scale=”

  bcsqrt — 求高精度数字平方根

  bcsub — 将两个高精度数字相减

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Fraction类的实现: ```java public class Fraction { private int f1; // 分子 private int f2; // 分母 // 构造方法 public Fraction(int f1, int f2) { this.f1 = f1; this.f2 = f2; } // 分数相加 public Fraction add(Fraction other) { int newF1 = this.f1 * other.f2 + other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相减 public Fraction subtract(Fraction other) { int newF1 = this.f1 * other.f2 - other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相乘 public Fraction multiply(Fraction other) { int newF1 = this.f1 * other.f1; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相除 public Fraction divide(Fraction other) { int newF1 = this.f1 * other.f2; int newF2 = this.f2 * other.f1; return new Fraction(newF1, newF2); } // 以a/b的形式打印分数 public String toString() { return f1 + "/" + f2; } // 以浮点数的形式打印分数 public double toDouble() { return (double) f1 / f2; } // 测试Fraction类 public static void main(String[] args) { Fraction f1 = new Fraction(1, 2); Fraction f2 = new Fraction(3, 4); System.out.println(f1.add(f2)); // 5/4 System.out.println(f1.subtract(f2)); // -1/4 System.out.println(f1.multiply(f2)); // 3/8 System.out.println(f1.divide(f2)); // 2/3 System.out.println(f1.toString()); // 1/2 System.out.println(f1.toDouble()); // 0.5 } } ``` 在主方法中,我们创建了两个分数对象f1和f2,并对它们进行了加、减、乘、除等运算,并使用toString()方法以a/b的形式打印分数,使用toDouble()方法以浮点数的形式打印分数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值