Why plot of a sinusoid with a large phase appears like a staircase?

85 Views Asked by At

I plotted the following in MATLAB and Desmos:

y = cos(x + 6998666554443343) (1)

The plot is shown here: Plot of (1)

This staircase behaviour seems to appear with any large number. The following plot with an extra digit in its phase is another example:

y = cos(x + 69986665544433456) (2) Plot of (2)

I think that I am crossing the limits of resolution somehow. Can someone explain the reason behind this 'staircase' behaviour?

2

There are 2 best solutions below

1
On BEST ANSWER

Matlab natively uses double precision floating point. As floating point values grow in magnitude the spacing between consecutive representable values gets larger. This spacing is referred to as machine epsilon and can be evaluated via eps in Matlab. For example, eps(6998666554443343) returns 1, which explains your first stair-stepped plot. eps(69986665544433456) returns 8, which is why your second plot is even coarser. Double precision only supports consecutive integer values up to 2^53 (see flintmax), which is ~9.0072e+15.

2
On

The large number you add is approximately $\approx10^{15}$. In binary this is close to $2^{15\log_2(10)}\approx2^{50}$. If the program is storing these as floats with $32$ bit precision, there will be a rounding error because it would require $50>32$ bits to store the number exactly. So, the number $x+6998666554443343$ is only kept to the first 32 bits, after which there is an error from rounding. The rounding function is discontinuous and looks like a staircase, jumping up in value suddenly.

If the underlying code switched to $64$-bit floats, the problem would go away.