Jump to content

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


Lightmeow
 Share

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:

 

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

 

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?

 

[latex] f(x)= sin \lfloor x \rfloor [/latex]

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

 

Thanks in advance for the help!

Link to comment
Share on other sites

tablsin.gif

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
Link to comment
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

Link to comment
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.

Link to comment
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

 

[math]\sin{\frac{\pi}{2}} = 1,\quad \sin{\pi} = 0,\quad \sin{0} = 0,\quad \sin{-\pi} = 0,\quad \sin{-\frac{\pi}{2}} = -1[/math],

 

and venture to form a function whose restrictions

 

[math]\{-\pi, 0, \pi\},\quad 0<x\ \land\ x \ne \pi,\quad x<0\ \land\ x \ne -\pi[/math]

 

respectively have images

 

[math]\pi,\quad \frac{\pi}{2},\quad -\frac{\pi}{2}[/math]

 

such as

 

[math]f(x) = (x \bmod \pi)+\pi[/math]

 

where the first case is satisfied,

 

[math]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}}[/math]

 

where the first and second cases are satisfied,

 

[math]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}})[/math]

 

where all three cases are satisfied.

 

So your sought for function of sine is

 

[math]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}})}[/math]

 

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

 

[math]a \bmod b = a - b * \lfloor \frac{a}{b} \rfloor[/math]

 

The ceiling function [math]\lceil x \rceil[/math] 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.

Link to comment
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
Link to comment
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?

[latex] 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}} [/latex]
Edited by Lightmeow
Link to comment
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, [math]\sin x \approx x[/math].

 

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
Link to comment
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?

[latex] 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}} [/latex]

 

 

Sure,

 

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

 

[math] (x \bmod \pi)+\pi [/math]

 

did as needed.

 

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

 

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

 

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

 

[math]a + b - a[/math]

 

Or,

 

[math]((x \bmod \pi)+\pi) + \frac{\pi}{2} - ((x \bmod \pi)+\pi)[/math]

 

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

 

So we want encode that condition as arithmetic, in our expression. That whenever it is 0 or [math]\pi[/math], 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 [math]\pi[/math] or 0. Well, we know that whenever we have one of those values,

 

[math]( (x \bmod \pi)+\pi) [/math]

 

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

 

[math] x \bmod \pi [/math]

 

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

 

[math] ((x \bmod \pi)+\pi) + (x \bmod \pi) * (\frac{\pi}{2} - ((x \bmod \pi)+\pi)) [/math]

 

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 [math]\pi[/math]. 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 ([math] x \bmod \pi [/math]).

 

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

 

[math](a + \frac{c(b - a)}{c})[/math]

 

[math]...[/math]

 

[math]((x \bmod \pi)+\pi) + (x \bmod \pi) * \frac{(\frac{\pi}{2} - (x \bmod \pi)+\pi)}{(x \bmod \pi)}[/math]

 

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 [math]0^0 = 1[/math]. We know that by [math]c = 0[/math], we mean [math](x \bmod \pi) = 0[/math]. So if we raise it to itself,

 

[math](x \bmod \pi)^{(x \bmod \pi)}[/math]

 

it will evaluate to 1 ([math]0^0[/math]). 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—[math]\lceil 0 \rceil = 0[/math]! So now we have a general way to take a value and return either 0 or 1, so long as it's [math] \geq 0 [/math] and [math] \leq 1 [/math].

 

We take our specific values and know that some modular operation [math]n \bmod m[/math] cannot evaluate to anything greater than m, so [math] x \bmod \pi [/math] will always be lesser than 3.14159... We also know that given two numbers i and j, if i < j, [math]\frac{i}{j}[/math] 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 [math]\lceil c \rceil[/math], where whenever it is 0, it will stay 0, and whenever it is a decimal, it will be 1. The expontent comes out as

 

[math]\lceil \frac{x \bmod \pi}{4}\rceil[/math]

 

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

 

[math] ((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}} [/math]

 

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.

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.