]> granicus.if.org Git - graphviz/commitdiff
dijkstra.c: use standard 'FLT_MAX' instead of 'MAXFLOAT'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 19 Dec 2021 19:37:41 +0000 (11:37 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 19 Dec 2021 23:17:45 +0000 (15:17 -0800)
This code is simply relying on some initial value that is “larger than any
reasonable result from the following computation.” So `FLT_MAX` is clearer and
more portable.

lib/neatogen/dijkstra.c

index d55f7eb22cf867f0805b0afb907b402c252f7b1a..37b1e7ce825494f0d8f87cd45fd69dac4bbb7fb9 100644 (file)
@@ -18,6 +18,7 @@
 ******************************************/
 
 #include <common/memory.h>
+#include <float.h>
 #include <neatogen/bfs.h>
 #include <neatogen/dijkstra.h>
 #include <limits.h>
@@ -343,7 +344,7 @@ void dijkstra_f(int vertex, vtx_data * graph, int n, float *dist)
 
     /* initial distances with edge weights: */
     for (i = 0; i < n; i++)
-       dist[i] = MAXFLOAT;
+       dist[i] = FLT_MAX;
     dist[vertex] = 0;
     for (i = 1; i < graph[vertex].nedges; i++)
        dist[graph[vertex].edges[i]] = graph[vertex].ewgts[i];
@@ -352,7 +353,7 @@ void dijkstra_f(int vertex, vtx_data * graph, int n, float *dist)
 
     while (extractMax_f(&H, &closestVertex, index, dist)) {
        closestDist = dist[closestVertex];
-       if (closestDist == MAXFLOAT)
+       if (closestDist == FLT_MAX)
            break;
        for (i = 1; i < graph[closestVertex].nedges; i++) {
            neighbor = graph[closestVertex].edges[i];
@@ -375,7 +376,7 @@ int dijkstra_sgd(graph_sgd *graph, int source, term_sgd *terms) {
     float *dists = N_GNEW(graph->n, float);
     int i;
     for (i=0; i<graph->n; i++) {
-        dists[i] = MAXFLOAT;
+        dists[i] = FLT_MAX;
     }
     dists[source] = 0;
     for (i=graph->sources[source]; i<graph->sources[source+1]; i++) {
@@ -387,7 +388,7 @@ int dijkstra_sgd(graph_sgd *graph, int source, term_sgd *terms) {
     int closest = 0, offset = 0;
     while (extractMax_f(&h, &closest, indices, dists)) {
         float d = dists[closest];
-        if (d == MAXFLOAT) {
+        if (d == FLT_MAX) {
             break;
         }
         // if the target is fixed then always create a term as shortest paths are not calculated from there