Skip to content

Commit

Permalink
Better floating point equality checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Sep 3, 2023
1 parent 0b528dc commit 18cc948
Show file tree
Hide file tree
Showing 40 changed files with 518 additions and 144 deletions.
2 changes: 2 additions & 0 deletions docs/licenses/License Attribution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ miniz library (gzip support) - Copyright (c) 2015 Wojciech Adam Koszek
BSD 3-Clause License
===========================================================================================

AlmostEquals Google Test Framework Component - Copyright (c) 2005 Google, Inc.

Blasphemer (various assets) - Copyright (c) 2021 Contributors to the Blasphemer project

Freedoom (various assets) - Copyright (c) 2001-2019 Contributors to the Freedoom project
Expand Down
1 change: 1 addition & 0 deletions source_files/ajbsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(edge_ajbsp
bsp_wad.cc
)

target_include_directories(edge_ajbsp PRIVATE ../almostequals)
target_include_directories(edge_ajbsp PRIVATE ../epi)
target_include_directories(edge_ajbsp PRIVATE ../miniz)

Expand Down
1 change: 1 addition & 0 deletions source_files/ajbsp/bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef __AJBSP_BSP_H__
#define __AJBSP_BSP_H__

#include "AlmostEquals.h"
#include <filesystem>

#define AJBSP_VERSION "1.04"
Expand Down
2 changes: 1 addition & 1 deletion source_files/ajbsp/bsp_misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ vertex_t *NewVertexDegenerate(vertex_t *start, vertex_t *end)
vert->x = start->x;
vert->y = start->x;

if (dlen == 0)
if (AlmostEquals(dlen, 0.0))
BugError("NewVertexDegenerate: bad delta!\n");

dx /= dlen;
Expand Down
18 changes: 9 additions & 9 deletions source_files/ajbsp/bsp_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ inline void ComputeIntersection(seg_t *seg, seg_t *part,
double perp_c, double perp_d, double *x, double *y)
{
// horizontal partition against vertical seg
if (part->pdy == 0 && seg->pdx == 0)
if (AlmostEquals(part->pdy, 0.0) && AlmostEquals(seg->pdx, 0.0))
{
*x = seg->psx;
*y = part->psy;
return;
}

// vertical partition against horizontal seg
if (part->pdx == 0 && seg->pdy == 0)
if (AlmostEquals(part->pdx, 0.0) && AlmostEquals(seg->pdy, 0.0))
{
*x = part->psx;
*y = seg->psy;
Expand All @@ -231,12 +231,12 @@ inline void ComputeIntersection(seg_t *seg, seg_t *part,
// 0 = start, 1 = end
double ds = perp_c / (perp_c - perp_d);

if (seg->pdx == 0)
if (AlmostEquals(seg->pdx, 0.0))
*x = seg->psx;
else
*x = seg->psx + (seg->pdx * ds);

if (seg->pdy == 0)
if (AlmostEquals(seg->pdy, 0.0))
*y = seg->psy;
else
*y = seg->psy + (seg->pdy * ds);
Expand Down Expand Up @@ -536,7 +536,7 @@ double EvalPartition(quadtree_c *tree, seg_t *part, double best_cost)
// partition lines that lie either purely horizontally or
// purely vertically.

if (part->pdx != 0 && part->pdy != 0)
if (!AlmostEquals(part->pdx, 0.0) && !AlmostEquals(part->pdy, 0.0))
info.cost += 25.0;

return info.cost;
Expand All @@ -552,7 +552,7 @@ void EvaluateFastWorker(quadtree_c *tree,
if (part->linedef == NULL)
continue;

if (part->pdy == 0)
if (AlmostEquals(part->pdy, 0.0))
{
// horizontal seg
if (! *best_H)
Expand All @@ -568,7 +568,7 @@ void EvaluateFastWorker(quadtree_c *tree,
*best_H = part;
}
}
else if (part->pdx == 0)
else if (AlmostEquals(part->pdx, 0.0))
{
// vertical seg
if (! *best_V)
Expand Down Expand Up @@ -1154,7 +1154,7 @@ int quadtree_c::OnLineSide(const seg_t *part) const
int p1, p2;

// handle simple cases (vertical & horizontal lines)
if (part->pdx == 0)
if (AlmostEquals(part->pdx, 0.0))
{
p1 = (tx1 > part->psx) ? +1 : -1;
p2 = (tx2 > part->psx) ? +1 : -1;
Expand All @@ -1165,7 +1165,7 @@ int quadtree_c::OnLineSide(const seg_t *part) const
p2 = -p2;
}
}
else if (part->pdy == 0)
else if (AlmostEquals(part->pdy, 0.0))
{
p1 = (ty1 < part->psy) ? +1 : -1;
p2 = (ty2 < part->psy) ? +1 : -1;
Expand Down
2 changes: 1 addition & 1 deletion source_files/ajbsp/bsp_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ double ComputeAngle(double dx, double dy)
{
double angle;

if (dx == 0)
if (AlmostEquals(dx, 0.0))
return (dy > 0) ? 90.0 : 270.0;

angle = atan2((double) dy, (double) dx) * 180.0 / M_PI;
Expand Down
Loading

0 comments on commit 18cc948

Please sign in to comment.