# How to make the sine(x) return only -1, 0, and 1

## Recommended Posts

I'm trying to write a function that where the domain is all whole numbers that returns only the values 1, 0, and -1.

At first I made a peacewise function:

$f(x)=\left\{\begin{matrix} -1 & x<0 \\ 0 & x=0\\ 1 &x>0 \end{matrix}\right.\$

But I think I can be more clever than that. Would it be proper to combine the floor function and the sine function to make this?

$f(x)= sin \lfloor x \rfloor$

Or would I need to do something else to make that work?

Thanks in advance for the help!

##### Share on other sites

Sine is defined on your calculator by this lookup table. All sine numbers including decimals can be extrapolated. decimals use the numbers 1-9 and are added. So Sin(1)=0.0174 , Sin(0.1)=0.00174 then Sin(1.1)=0.0174+0.00174

Edited by fiveworlds
##### Share on other sites

I'm trying to write a function that where the domain is all whole numbers that returns only the values 1, 0, and -1.

You need to include a statement of when the function is to return 1, 0 or -1

ie what is the specific condition on x for f(x) = 1 etc

##### Share on other sites

Sine is defined on your calculator by this lookup table.

I doubt it very much.

All sine numbers including decimals can be extrapolated. decimals use the numbers 1-9 and are added. So Sin(1)=0.0174 , Sin(0.1)=0.00174 then Sin(1.1)=0.0174+0.00174

None of which is in the slightest bit relevant to the question.

##### Share on other sites

The piecewise function looks just fine, and cleaner than something more clever or convoluted.

But, if you want to apply sine, you could take your knowledge that

$\sin{\frac{\pi}{2}} = 1,\quad \sin{\pi} = 0,\quad \sin{0} = 0,\quad \sin{-\pi} = 0,\quad \sin{-\frac{\pi}{2}} = -1$,

and venture to form a function whose restrictions

$\{-\pi, 0, \pi\},\quad 0<x\ \land\ x \ne \pi,\quad x<0\ \land\ x \ne -\pi$

respectively have images

$\pi,\quad \frac{\pi}{2},\quad -\frac{\pi}{2}$

such as

$f(x) = (x \bmod \pi)+\pi$

where the first case is satisfied,

$f(x) = ((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}}$

where the first and second cases are satisfied,

$f(x) = \frac{x}{\left| x \right|^{\lceil \frac{x \bmod \pi}{4}\rceil}}(((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}})$

where all three cases are satisfied.

So your sought for function of sine is

$f(x) = \sin{\frac{x}{\left| x \right|^{\lceil \frac{x \bmod \pi}{4}\rceil}}(((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}})}$

There's your intended function as a function of sine. Note that the mod operator returns the remainder after a division and can be rewritten into an expression involving only the floor function

$a \bmod b = a - b * \lfloor \frac{a}{b} \rfloor$

The ceiling function $\lceil x \rceil$ is like the floor function but it instead chooses the next greatest integer.

I used mod and ceiling for notational convenience, as otherwise the equations would be much more clunky.

Hope this was clear! It was very tedious to come up with, and in the end it is identical to the simple piecewise function. I'd say, stick with simplicity; don't go for the more complicated "clever" solution, if a simpler, more elegant one is available to you.

##### Share on other sites

I doubt it very much.

Calculators use Cordic algorithms using add,subtract,multiply and table lookup. There is supposedly an algorithm that calculates sine but it wasn't used because early calculators didn't have the memory. They stored one quarter sine wave as a lookup table in memory.

Edited by fiveworlds
##### Share on other sites

Thanks Sato, that must have taken some time to think of! I wasn't expecting it to get that complacated, although math can get as complex and hard as you want... If it wouldn't take to much time, could you explain to me how you got to the finial answer?

Like how do you know that this will come out with the final answer: If its to tedious and time consuming don't bother, but I am cerious...

How can you explain you came up with the below equation?

$f(x) = ((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}}$
Edited by Lightmeow
##### Share on other sites

Sine is defined on your calculator by this lookup table. All sine numbers including decimals can be extrapolated. decimals use the numbers 1-9 and are added. So Sin(1)=0.0174 , Sin(0.1)=0.00174 then Sin(1.1)=0.0174+0.00174

The above is low in error for small x, because for low x, $\sin x \approx x$.

But for values of not low x, the above linearization is rather poor in accuracy. Eg. using your method to calculate sin(40.1 degrees) would have an error of about 0.05%. That may be good enough or horribly, horribly inaccurate depending on your application. I would be pretty disappointed in my calculator to have that large of an error; it was probably acceptable when calculators were first replacing slide rules, but wouldn't really be acceptable today.

The above may have been true of the very first calculators, but it isn't true for modern ones. Modern calculators have errors in 1 part per million or better.

Edited by Bignose
##### Share on other sites

Thanks Sato, that must have taken some time to think of! I wasn't expecting it to get that complacated, although math can get as complex and hard as you want... If it wouldn't take to much time, could you explain to me how you got to the finial answer?

Like how do you know that this will come out with the final answer: If its to tedious and time consuming don't bother, but I am cerious...

How can you explain you came up with the below equation?

$f(x) = ((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}}$

Sure,

first, as I said in the formulation, I needed to satisfy the first condition, so found that

$(x \bmod \pi)+\pi$

did as needed.

So, that would turn 0 and $n\pi$ into $\pi$, which would be evaluated as $\sin{\pi}$ to be 0.

Then, to satisfy condition two, if it wasn't turned into $\pi$, it should be turned into $\frac{\pi}{2}$, which would be evaluated as $\sin{\frac{\pi}{2}}$ to be 1.

So, given some value a ($(x \bmod \pi)+\pi$) that is stuck in an expression, and a value b ($\frac{\pi}{2}$) that we want to turn that expression into, what sequence of operations can we use to achieve our goal?

$a + b - a$

Or,

$((x \bmod \pi)+\pi) + \frac{\pi}{2} - ((x \bmod \pi)+\pi)$

But, unfortunately we find that our first condition is no longer satisfied; it is now always $\frac{\pi}{2}$, and no longer is it $\pi$ at 0 and $n\pi$.

So we want encode that condition as arithmetic, in our expression. That whenever it is 0 or $\pi$, the replacement of a with b shouldn't happen. We want a value c that can turn off the + b - a. Where a + c(b - a) equals a (that is, c equals 0), whenever we have $\pi$ or 0. Well, we know that whenever we have one of those values,

$( (x \bmod \pi)+\pi)$

evaluates to $\pi$ as per condition one. So to have it evaluate to 0 when given those values, we'd subtract $\pi$, which given our expression is not so hard. We then have:

$x \bmod \pi$

So that is our c. We then form a + c(b - a), or

$((x \bmod \pi)+\pi) + (x \bmod \pi) * (\frac{\pi}{2} - ((x \bmod \pi)+\pi))$

This seems good but we find that whenever c doesn't evaluate to 0, it evaluates to the remainder after x (our input) is divided by $\pi$. We want c to evaluate to 1 so that c(b - a) is just b - a, but now it's b - a multiplied by some strange number ($x \bmod \pi$).

We first consider undoing the effects of c by dividing it out

$(a + \frac{c(b - a)}{c})$

$...$

$((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)}$

But then it appears that this would make c no longer able in its original use. We want to divide it out for case two where it interferes, but for case one, when c*(b - a) should evaluate to 0, we want c to keep its effect (multiplying by 0). We then notice that 0 cannot be divided out, so case one's (0*(b - a)) / 0 does not have the problem that multiplication by 0 is being undone, but that the expression is being divided by 0, which is undefined and prohibits us from moving on.

We contemplate a solution and become aware our actual goal is to make the denominator non-zero whenever c is 0, and have the denominator be equal to just c whenever c is non-zero (because that means we're in condition two). How can we do this? One idea that comes to mind is that $0^0 = 1$. We know that by $c = 0$, we mean $(x \bmod \pi) = 0$. So if we raise it to itself,

$(x \bmod \pi)^{(x \bmod \pi)}$

it will evaluate to 1 ($0^0$). However, although this satisfies case one where c will be 0, case two, where c must be divided out, is no longer functioning. Rather, cc is the divisor. This obviously doesn't achieve the same result as dividing by c, as our original "fix" intended. So, we want to raise c in the denominator to some exponent that is equal to c whenever c is 0 (so cc will be 1, avoiding division by 0), and that is equal to 1 whenever c is not 0 (so c1 is just c, as needed for case two).

A wild light bulb appears. We recall the ceiling operation, which takes a decimal number and returns the closest integer greater than it. It comes to mind that if you were to apply the ceiling function to any number greater than 0 and less than or equal to 1, it would always return 1. And if you apply it to an integer it returns that same integer—$\lceil 0 \rceil = 0$! So now we have a general way to take a value and return either 0 or 1, so long as it's $\geq 0$ and $\leq 1$.

We take our specific values and know that some modular operation $n \bmod m$ cannot evaluate to anything greater than m, so $x \bmod \pi$ will always be lesser than 3.14159... We also know that given two numbers i and j, if i < j, $\frac{i}{j}$ will be less than 1. So we can take our c and divide it by some value greater than what c can be, and we will always get a number greater than or equal to 0 and less than 1 (so long as c is non-negative, which it is). We can choose something like 3.15, but 4 is cleaner, so we divide our c by 4. If c is 0, dividing it by 4 will have no effect and will leave it at 0. But, if c > 0, c divided by 4 will yield a decimal greater than 0 but less than one.

Our goal is to make it just c, and so we want to raise our value, whenever it's not 0, to 1. We do this easily by $\lceil c \rceil$, where whenever it is 0, it will stay 0, and whenever it is a decimal, it will be 1. The expontent comes out as

$\lceil \frac{x \bmod \pi}{4}\rceil$

and the final result, putting all of our pieces together, is

$((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)^{\lceil \frac{x \bmod \pi}{4}\rceil}}$

I tried to recount my thought process in a very pedagogical way, so there is a good bit of repetition, but hopefully the whole processes as I convey it is comprehensive and comprehensible.

##### Share on other sites

Calculators use Cordic algorithms using add,subtract,multiply and table lookup. There is supposedly an algorithm that calculates sine but it wasn't used because early calculators didn't have the memory. They stored one quarter sine wave as a lookup table in memory.

Thanks for confirming that your previous post was incorrect.

##### Share on other sites

Is there some confusion here between sine and sign?

http://en.wikipedia.org/wiki/Sign_function

##### Share on other sites

Is there some confusion here between sine and sign?

http://en.wikipedia.org/wiki/Sign_function

I'm talking about the trig function. Although the sign function looks like it wants to do what I want it to.

Edited by Lightmeow

## Create an account

Register a new account