raytracing.github.io icon indicating copy to clipboard operation
raytracing.github.io copied to clipboard

[Book 1] Listing 26 Section 6.8 Infinity is undefined

Open solimm4sks opened this issue 2 years ago • 1 comments

In the listing:

#ifndef INTERVAL_H
#define INTERVAL_H

class interval {
  public:
    double min, max;

    interval() : min(+infinity), max(-infinity) {} // Default interval is empty

    interval(double _min, double _max) : min(_min), max(_max) {}

    bool contains(double x) const {
        return min <= x && x <= max;
    }

    bool surrounds(double x) const {
        return min < x && x < max;
    }

    static const interval empty, universe;
};

const static interval empty   (+infinity, -infinity);
const static interval universe(-infinity, +infinity);

#endif

The file rtweekend.h is not included, and thus infinity is undefined. This was mentioned in #1281 but the issue was closed and the listing is still "wrong".

solimm4sks avatar Dec 05 '23 19:12 solimm4sks

I've run into this as well.

The issue is the common headers need to be included after the double infinity definition.

...

// Common Headers

#include "interval.h"
#include "ray.h"
#include "vec3.h"

#endif

Not so much an issue, but could be worth pointing out.

rupsis avatar Dec 06 '23 21:12 rupsis

Having interval.h include rtweekend.h would induce a circular include loop, since rtweekend.h includes interval.h. I suspect that your version of rtweekend.h lacks the inclusion mentioned in book 1, listing 27 ("Including the new interval class").

hollasch avatar Feb 20 '24 09:02 hollasch

@solimm4sks — please let me know if I've missed something.

hollasch avatar Feb 20 '24 22:02 hollasch

It seems to me that infinity should be defined in interval.h instead of in rtweekend.h, which is where it is currently defined by the time one reaches Book 1 Section 6.8. This would solve the undefined issue, avoid the circular dependency, and still ensure that the const is accessible wherever rtweekend.h is included.

bickybee avatar Mar 21 '24 01:03 bickybee

Infinity is not a concept limited to intervals only. It is also used, for example in the camera code. Readers are instructed to always include rtweekend.h as the first header file, which addresses this issue. The original poster missed that.

hollasch avatar Mar 21 '24 18:03 hollasch