Float Equal Problem

Understand limitations of floating point representations.Never check for equality with ==. Instead, check if the difference is less than an epsilon value.

public class Line{
    static double epsilon=0.000001;
    public double slope;
    public double yintersect;

    public Line(double s, double y){
            slope=s;
            yintersect=y;
    }

    public boolean intersect(Line line2){
            return Math.abs(slope-line2.slope)>epsilon||
            Math.abs(yintersect-line2.yintersect)<epsilon;
    }
}

  

Float Equal Problem

时间: 2024-11-03 13:21:30

Float Equal Problem的相关文章

[LeetCode&amp;Python] Problem 453. Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

cs224d problem set2 (三) 用RNNLM模型实现Language Model,来预测下一个单词的出现

今天将的还是cs224d 的problem set2 的第三部分习题, 原来国外大学的系统难度真的如此之大,相比之下还是默默地再天朝继续搬砖吧 (注意前方高能,一大批天书即将来袭) ''' Created on 2017年9月26日 @author: weizhen ''' import getpass import sys import time import numpy as np from copy import deepcopy from utils import calculate_p

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

[算法]K-SUM problem

一.Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please

Problem C: Celebrity Split

题目描写叙述 Problem C: Celebrity Split Jack and Jill have decided to separate and divide their property equally. Each of their N mansions has a value between 1,000,000 and 40,000,000 dollars. Jack will receive some of the mansions; Jill will receive some

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 4227 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC.

java中==与equal()的区别

==和equal()都是用来判断两个变量是否相等的. (1)如果两个变量是基本类型变量,且都是数值型的(不一定数据类型相同),只要是值相同,将返回true; (2)如果两个变量是引用型变量,只有它们指向同一个对象时,==才返回true; (3)equal比较的是两个字符串,只要两个字符串的字符对应相等,就返回true. int it=65; float f1=65.0f; //it==f1 为true char ch=‘A’; //it==ch 为true String str1 = new S

Problem A: Artificial Intelligence?

Description Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first! So they don't state a problem like ``U=10V, I=5A, P=?"

UPC 2170 D Equal Is Not Really Equal (欧拉路径)

题目链接:http://acm.upc.edu.cn/problem.php?id=2170 题意:给出一个字符串,比如ABACA,在这个串里,AB.BA.AC.CA各出现一次.若存在另外一个串,里面也AB.BA.AC.CA各出现一次.我们称ABACA是不唯一的.给出一个串,判断其是不是唯一. 思路:将字母看做顶点,将相邻的连有向边.那么题 目转化成这个图是不是存在两条欧拉路径.现在这个图至少有一条欧拉路径.设原串最后一个字母对应的节点为end.那么现在的图中若存在一个点u,对于这个 u至少有两