Spivak's Calculus, Ch 13 "Integrals, Prob *27: $f$ integrable on $[a,b]$ there are cont. fns $g\leq f \leq h$ with $\int_a^b h - \int_a^b g<\epsilon$. The following is a problem from Chapter 13 "Integrals" from Spivak's Calculus

*27. Prove that if $f$ is integrable on $[a,b]$, then for any $\epsilon>0$ there are continuous functions $g\leq f \leq h$ with
$\int_a^b h-\int_a^b g<\epsilon$. Hint: First get step functions with
this property, and then continuous ones. A picture will help
immensely.

The solution manual says

It obviously suffices to show that for any $\epsilon>0$ there are
$g\leq f$ with $\int_a^b f - \int_a^b g < \epsilon$ and $h \geq f$
with $\int_a^b h - \int_a^b f<\epsilon$. Moreover, the second follows
from the first by considering $-f$, so we just have to find the
desired $g \leq f$.
Chose a step function $s \leq f$ with $\int_a^bf - \int_a^b
 s<\epsilon/2$, by Problem 26(a). Choose $M\geq 1$ so that $|f(x)|\leq
 M$ for all $x$ in $[a,b]$, and if $s$ is constant on $(t_{i-1},t_i)$
for $i=1,...,n$, choose $\delta<\frac{\epsilon}{2nM}$. Let $g=s$ on
$[t_{i-1}+\frac{\delta}{2}, t_i-\frac{\delta}{2}]$ and let $g$ be a
linear function on $[t_i-\frac{\delta}{2},t_i]$ and $[t_i,
 t_i+\frac{\delta}{2}]$, with $g(t_i)=-M$.
Then $g\leq s \leq f$ and $\int_a^b s - \int_a^b g \leq
 nM\delta<\epsilon/2$ so $\int_a^b f - \int_a^b g <\epsilon$.

My question is about the very last line in the above proof. Let me go through the proof again, but with more intermediate steps to make sure the reasoning is all correct.
If $f$ is integrable, we can always find a step function $s \leq f$ such that $\int_a^b f - \int_a^b s < \frac{\epsilon}{4}$ (this is a result proved in a previous problem, 26a).
I'm using $\frac{\epsilon}{4}$ here instead of $\frac{\epsilon}{2}$, so that in the end we obtain $\int_a^b f - \int_a^b g < \frac{\epsilon}{2}$ which, when combined with $\int_a^b h - \int_a^b f < \frac{\epsilon}{2}$ gives us our desired result: $\int_a^b h - \int_a^b g < \epsilon$.
Since $f$ is integrable (see edit below for definition of "integrable" being used), it is bounded, and there is some $M \geq 1$ such that
$$\forall x, x \in [a,b] \implies |f(x)|\leq M$$
Given a partition $P=\{t_0,...,t_n\}$, $s$ is constant on $(t_{i-1},t_i)$, for $i=1,...,n$.
Let $\delta=\frac{\epsilon}{4nM}$.
Now let's define a function $g$. For $i=1,...,n$, let

*

*$g=s$ on $[t_{i-1}+\frac{\delta}{2}, t_i-\frac{\delta}{2}]$

*$g$ linear on $[t_i-\frac{\delta}{2},t_i]$ and $[t_i, t_i+\frac{\delta}{2}]$, with $g(t_i)=-M$.

For the $i^{th}$ partition subinterval, $[t_{i-1},t_i]$, I believe $g$ looks something like the following

And this is what I believe $g$ looks like for an entire interval $[a,b]$

So here is the part my question is about.
Let $s_i$ denote the constant value of the step function $s$ in the $i^{th}$ partition subinterval. Then
$$\int_{t_{i-1}}^{t_i} s = s_i (t_i-t_{i-1})=s_i \Delta t_i$$
$$\int_{t_{i-1}}^{t_i} g = \left [ (t_i+\frac{\delta}{2})-(t_{i-1}+\frac{\delta}{2}) \right ] \cdot s_i + N_i$$
$$=s_i\Delta t_i -s_i\delta +N_i$$
where $N_i$ corresponds to the integral of $g$ on the increasing and decreasing portions in a subinterval, ie on the edges of a subinterval.
I believe $N_i<0$. I won't prove it here, but unless I am mistaken, it is pretty clear it must be so.
Now, if this $N_i$ term did not exist here, it seems we'd have exactly the same solution as the solution manual, namely
$$\int_{t_{i-1}}^{t_i} s-\int_{t_{i-1}}^{t_i} g = s_i\delta\leq M\delta=\frac{\epsilon}{4n}$$
Since we have $n$ subintervals then
$$\int_a^b s-\int_a^b g \leq n\frac{\epsilon}{4n}=\frac{\epsilon}{4}$$
But the term $N_i$ does exist in each subinterval. Is there a mistake in my reasoning?
On the other hand, it also seems clear that we could assert that
$$\int_{t_{i-1}}^{t_i} s-\int_{t_{i-1}}^{t_i} g < 2M\delta=\frac{\epsilon}{2n}$$
so
$$\int_a^b s-\int_a^b g \leq n\frac{\epsilon}{2n}=\frac{\epsilon}{2}$$
How did the solution manual come up with $\int_a^b s - \int_a^b g \leq
 nM\delta<\frac{\epsilon}{2}$?
Edit: the definition of integral used is the one present in Spivak's Calculus.

Definition: A function $f$ which is bounded on $[a,b]$ is integrable on $[a,b]$ if $\sup\{L(f,P)\}=\inf\{U(f,P)\}$. This common
number is called the integral of $f$ on $[a,b]$.

 A: First we split the integral into three parts: the left edge where g is increasing, the middle portion where it is constant and coincides with the step function, and the right edge where it decreases down to -M.
$$\int\limits_{t_{i-1}}^{t_i} g= \int\limits_{t_{i-1}}^{t_{i-1}+\frac{\delta}{2}}g + \int\limits_{t_{i-1}+\frac{\delta}{2}}^{t_i-\frac{\delta}{2}}g + \int\limits_{t_i-\frac{\delta}{2}}^{t_i}g$$
$$= \int\limits_{t_{i-1}}^{t_{i-1}+\frac{\delta}{2}}g + \left [ (t_i-\frac{\delta}{2})-(t_{i-1}+\frac{\delta}{2}) \right ]s_i + \int\limits_{t_i-\frac{\delta}{2}}^{t_i}g$$
$$= \int\limits_{t_{i-1}}^{t_{i-1}+\frac{\delta}{2}}g + \left [ t_i-t_{i-1}-\delta \right ]s_i + \int\limits_{t_i-\frac{\delta}{2}}^{t_i}g$$
However, it is also true that
$$\int\limits_{t_i-\frac{\delta}{2}}^{t_i}g \leq 2|s_i| \frac{\delta}{2}$$
and
$$\int\limits_{t_{i-1}}^{t_{i-1}+\frac{\delta}{2}}g \leq 2|s_i| \frac{\delta}{2}$$
and
$$\left [ t_i-t_{i-1}-\delta \right ]s_i \leq \left [ t_i-t_{i-1}-\delta \right ] |s_i|$$
Therefore
$$\int\limits_{t_{i-1}}^{t_i} g \leq |s_i| \Delta t_i - |s_i| \delta + 2|s_i| \frac{\delta}{2}+ 2|s_i| \frac{\delta}{2}$$
$$=|s_i| \Delta t_i+|s_i|\delta$$
Then we take the difference
$$\int\limits_{t_{i-1}}^{t_i} s-\int\limits_{t_{i-1}}^{t_i} g=|s_i| \delta \leq M \delta = M \frac{\epsilon}{4nM}=\frac{\epsilon}{4n}$$
Since we have n subintervals in the partition, we can assert that
$$\int\limits_a^b s-\int\limits_a^b g=  \sum\limits_{i=1}^n |s_i| \delta \leq \sum\limits_{i=1}^n \frac{\epsilon}{4n}= n\frac{\epsilon}{4n}=\frac{\epsilon}{4}$$
