Skip to content

Commit 5ff6e7a

Browse files
committed
Use explicit std:: namespace almost everywhere
When referring to C++ standard library functions, use the explicit `std::` namespace qualifier. The only exception is that we bring `shared_ptr` and `make_shared` into the global namespace in order to keep the code brief, since we have a 96-character line limit in our source code. Currently, this covers the following identifiers: acos fixed numeric_limits sqrt atan2 floor ostream string cerr flush rand tan clog fmax setprecision vector cos fmin shared_ptr cout log sin fabs make_shared sort Resolves #1487
1 parent 0867c84 commit 5ff6e7a

36 files changed

+198
-223
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,6 @@
310310
#include <cmath>
311311
#include <iostream>
312312

313-
using std::sqrt;
314-
315313
class vec3 {
316314
public:
317315
double e[3];
@@ -346,7 +344,7 @@
346344
}
347345

348346
double length() const {
349-
return sqrt(length_squared());
347+
return std::sqrt(length_squared());
350348
}
351349

352350
double length_squared() const {
@@ -968,7 +966,7 @@
968966
if (discriminant < 0) {
969967
return -1.0;
970968
} else {
971-
return (-b - sqrt(discriminant) ) / (2.0*a);
969+
return (-b - std::sqrt(discriminant) ) / (2.0*a);
972970
}
973971
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
974972
}
@@ -1013,7 +1011,7 @@
10131011
if (discriminant < 0) {
10141012
return -1.0;
10151013
} else {
1016-
return (-b - sqrt(discriminant) ) / (2.0*a);
1014+
return (-b - std::sqrt(discriminant) ) / (2.0*a);
10171015
}
10181016
}
10191017
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1055,7 +1053,7 @@
10551053
return -1.0;
10561054
} else {
10571055
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1058-
return (h - sqrt(discriminant)) / a;
1056+
return (h - std::sqrt(discriminant)) / a;
10591057
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10601058
}
10611059
}
@@ -1120,7 +1118,7 @@
11201118

11211119
class sphere : public hittable {
11221120
public:
1123-
sphere(const point3& center, double radius) : center(center), radius(fmax(0,radius)) {}
1121+
sphere(const point3& center, double radius) : center(center), radius(std::fmax(0,radius)) {}
11241122

11251123
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
11261124
vec3 oc = center - r.origin();
@@ -1132,7 +1130,7 @@
11321130
if (discriminant < 0)
11331131
return false;
11341132

1135-
auto sqrtd = sqrt(discriminant);
1133+
auto sqrtd = std::sqrt(discriminant);
11361134

11371135
// Find the nearest root that lies in the acceptable range.
11381136
auto root = (h - sqrtd) / a;
@@ -1158,6 +1156,9 @@
11581156
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11591157
[Listing [sphere-initial]: <kbd>[sphere.h]</kbd> The sphere class]
11601158

1159+
(Note here that we use the C++ standard function `std::fmax()`, which returns the maximum of the two
1160+
floating-point arguments. Similarly, we will later use `std::fmin()`, which returns the minimum of
1161+
the two floating-point arguments.)
11611162
</div>
11621163

11631164

@@ -1334,8 +1335,8 @@
13341335

13351336
Some New C++ Features
13361337
----------------------
1337-
The `hittable_list` class code uses two C++ features that may trip you up if you're not normally a
1338-
C++ programmer: `vector` and `shared_ptr`.
1338+
The `hittable_list` class code uses some C++ features that may trip you up if you're not normally a
1339+
C++ programmer: `vector`, `shared_ptr`, and `make_shared`.
13391340

13401341
`shared_ptr<type>` is a pointer to some allocated type, with reference-counting semantics. Every
13411342
time you assign its value to another shared pointer (usually with a simple assignment), the
@@ -1410,7 +1411,6 @@
14101411

14111412
using std::make_shared;
14121413
using std::shared_ptr;
1413-
using std::sqrt;
14141414

14151415
// Constants
14161416

@@ -1476,8 +1476,6 @@
14761476
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
14771477
#include <cmath>
14781478
#include <iostream>
1479-
1480-
using std::sqrt;
14811479
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14821480
[Listing [assume-rtw-vec3]: <kbd>[vec3.h]</kbd> Assume rtweekend.h inclusion for vec3.h]
14831481

@@ -1998,9 +1996,9 @@
19981996
return a canonical random number, which by convention falls in the range $0 ≤ n < 1$. The “less
19991997
than” before the 1 is important, as we will sometimes take advantage of that.
20001998

2001-
A simple approach to this is to use the `rand()` function that can be found in `<cstdlib>`, which
2002-
returns a random integer in the range 0 and `RAND_MAX`. Hence we can get a real random number as
2003-
desired with the following code snippet, added to `rtweekend.h`:
1999+
A simple approach to this is to use the `std::rand()` function that can be found in `<cstdlib>`,
2000+
which returns a random integer in the range 0 and `RAND_MAX`. Hence we can get a real random number
2001+
as desired with the following code snippet, added to `rtweekend.h`:
20042002

20052003
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20062004
#include <cmath>
@@ -2022,7 +2020,7 @@
20222020
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ Highlight
20232021
inline double random_double() {
20242022
// Returns a random real in [0,1).
2025-
return rand() / (RAND_MAX + 1.0);
2023+
return std::rand() / (RAND_MAX + 1.0);
20262024
}
20272025

20282026
inline double random_double(double min, double max) {
@@ -2742,7 +2740,7 @@
27422740
inline double linear_to_gamma(double linear_component)
27432741
{
27442742
if (linear_component > 0)
2745-
return sqrt(linear_component);
2743+
return std::sqrt(linear_component);
27462744

27472745
return 0;
27482746
}
@@ -2869,7 +2867,7 @@
28692867
class sphere : public hittable {
28702868
public:
28712869
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2872-
sphere(const point3& center, double radius) : center(center), radius(fmax(0,radius)) {
2870+
sphere(const point3& center, double radius) : center(center), radius(std::fmax(0,radius)) {
28732871
// TODO: Initialize the material pointer `mat`.
28742872
}
28752873
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -2953,22 +2951,8 @@
29532951
the vector is very close to zero in all dimensions.
29542952

29552953
The following changes will use the C++ standard library function `std::fabs`, which returns the
2956-
absolute value of its input, so add this to our standard header.
2954+
absolute value of its input.
29572955

2958-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2959-
// C++ Std Usings
2960-
2961-
2962-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2963-
using std::fabs;
2964-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2965-
using std::make_shared;
2966-
using std::shared_ptr;
2967-
using std::sqrt;
2968-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2969-
[Listing [declare-fabs]: <kbd>[rtweekend.h]</kbd> Declaring std::fabs()]
2970-
2971-
29722956
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
29732957
class vec3 {
29742958
...
@@ -2982,7 +2966,7 @@
29822966
bool near_zero() const {
29832967
// Return true if the vector is close to zero in all dimensions.
29842968
auto s = 1e-8;
2985-
return (fabs(e[0]) < s) && (fabs(e[1]) < s) && (fabs(e[2]) < s);
2969+
return (std::fabs(e[0]) < s) && (std::fabs(e[1]) < s) && (std::fabs(e[2]) < s);
29862970
}
29872971
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
29882972

@@ -3135,7 +3119,7 @@
31353119
public:
31363120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
31373121
sphere(const point3& center, double radius, shared_ptr<material> mat)
3138-
: center(center), radius(fmax(0,radius)), mat(mat) {}
3122+
: center(center), radius(std::fmax(0,radius)), mat(mat) {}
31393123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
31403124

31413125
...
@@ -3362,19 +3346,16 @@
33623346

33633347
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ Highlight
33643348
inline vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
3365-
auto cos_theta = fmin(dot(-uv, n), 1.0);
3349+
auto cos_theta = std::fmin(dot(-uv, n), 1.0);
33663350
vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
3367-
vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n;
3351+
vec3 r_out_parallel = -std::sqrt(std::fabs(1.0 - r_out_perp.length_squared())) * n;
33683352
return r_out_perp + r_out_parallel;
33693353
}
33703354
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33713355
[Listing [refract]: <kbd>[vec3.h]</kbd> Refraction function]
33723356

33733357
</div>
33743358

3375-
(Note here that we use the C++ standard function `fmin()`, which returns the minimum of the two
3376-
arguments. Similarly, we will later use `fmax()`, which returns the maximum of the two arguments.)
3377-
33783359
<div class='together'>
33793360
And the dielectric material that always refracts is:
33803361

@@ -3489,8 +3470,8 @@
34893470
$$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
34903471

34913472
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3492-
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
3493-
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
3473+
double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0);
3474+
double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta);
34943475

34953476
if (ri * sin_theta > 1.0) {
34963477
// Must Reflect
@@ -3517,8 +3498,8 @@
35173498

35183499
vec3 unit_direction = unit_vector(r_in.direction());
35193500
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
3520-
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
3521-
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
3501+
double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0);
3502+
double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta);
35223503

35233504
bool cannot_refract = ri * sin_theta > 1.0;
35243505
vec3 direction;
@@ -3603,8 +3584,8 @@
36033584
double ri = rec.front_face ? (1.0/refraction_index) : refraction_index;
36043585

36053586
vec3 unit_direction = unit_vector(r_in.direction());
3606-
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
3607-
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
3587+
double cos_theta = std::fmin(dot(-unit_direction, rec.normal), 1.0);
3588+
double sin_theta = std::sqrt(1.0 - cos_theta*cos_theta);
36083589

36093590
bool cannot_refract = ri * sin_theta > 1.0;
36103591
vec3 direction;
@@ -3745,7 +3726,7 @@
37453726
auto focal_length = 1.0;
37463727
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
37473728
auto theta = degrees_to_radians(vfov);
3748-
auto h = tan(theta/2);
3729+
auto h = std::tan(theta/2);
37493730
auto viewport_height = 2 * h * focal_length;
37503731
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
37513732
auto viewport_width = viewport_height * (double(image_width)/image_height);
@@ -3780,7 +3761,7 @@
37803761

37813762

37823763
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
3783-
auto R = cos(pi/4);
3764+
auto R = std::cos(pi/4);
37843765

37853766
auto material_left = make_shared<lambertian>(color(0,0,1));
37863767
auto material_right = make_shared<lambertian>(color(1,0,0));
@@ -3889,7 +3870,7 @@
38893870
auto focal_length = (lookfrom - lookat).length();
38903871
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
38913872
auto theta = degrees_to_radians(vfov);
3892-
auto h = tan(theta/2);
3873+
auto h = std::tan(theta/2);
38933874
auto viewport_height = 2 * h * focal_length;
38943875
auto viewport_width = viewport_height * (double(image_width)/image_height);
38953876

@@ -4140,7 +4121,7 @@
41404121
auto focal_length = (lookfrom - lookat).length();
41414122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
41424123
auto theta = degrees_to_radians(vfov);
4143-
auto h = tan(theta/2);
4124+
auto h = std::tan(theta/2);
41444125
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
41454126
auto viewport_height = 2 * h * focus_dist;
41464127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -4168,7 +4149,7 @@
41684149

41694150
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
41704151
// Calculate the camera defocus disk basis vectors.
4171-
auto defocus_radius = focus_dist * tan(degrees_to_radians(defocus_angle / 2));
4152+
auto defocus_radius = focus_dist * std::tan(degrees_to_radians(defocus_angle / 2));
41724153
defocus_disk_u = u * defocus_radius;
41734154
defocus_disk_v = v * defocus_radius;
41744155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

0 commit comments

Comments
 (0)