Binary floating-point number

1. In doing floating-point half time to keep in mind there is an empirical value, the title if we want to retain the 6 decimal, then we need to set eps 1e8, (experience: precise decimal point +2)

2. We write in binary code when you need to remember l = mid, r = mid;

 

Code:

#include <iostream>
#include <cstdio>
using namespace std;

const double eps = 1e-8;

inline double find(double l, double r, double x) {
	
	while(r - l > eps) {
		double mid = (l + r) / 2;
		if(mid * mid * mid >= x) r = mid;
		else l = mid; 
	}
	
	return l;
}

int main(void) {
	double n;
	scanf("%lf", &n);
	
	double t = find(-101, 101, n);
	
	printf("%.6lf\n", t);
	
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/phaLQ/p/11203003.html