# How to cube a sine/cosine function in processing

**URL:** <https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257>\
**Category:** Coding Questions\
**Created:** [January 22, 2020, 5:30pm UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257 "2020-01-22T17:30:34Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 11, 2020, 6:11pm UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/22 "2020-02-11T18:11:48Z")

</div>

Very cool!

For TRIANGLE\_FAN, that is producing a lot of little shapes. For one shape, also try:

```auto
# this is pseudocode:
beginShape
  vertex(center)
  for (0 <= 360 stepped)
    compute edgepoint
    vertex(edgepoint)
endShape

```

So there is only one vertex call in the loop, and the first /last point created by the loop will be the same.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 11, 2020, 8:08pm UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/23 "2020-02-11T20:08:49Z")

</div>

That worked!

```auto
  s = createShape();
  s.beginShape(TRIANGLE_FAN);
  s.vertex (0, 0, 0);
  for (int t = 1; t<steps; t++) 
    {
    float angle = t*(TAU)/steps;  
    // compute
    s.vertex(x, y, z);
    }
  s.endShape();
  shape(s, 0, 0);

```

Thank you.

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [July 6, 2020, 2:56am UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/24 "2020-07-06T02:56:43Z")

</div>

@trikaphundo  
Why does it compute it like this?

> [@trikaphundo](#):
>
> exp(y \* log(x))

Why not just like people might by using repeated multiplication.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 9, 2020, 3:39am UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/25 "2020-07-09T03:39:54Z")

</div>

Hello @Adi,

Using repeated multiplication only works for whole positive numbers (1, 2, 3, 4,…).

Using the definition with exp and log allows to use any real numbers so raising 5 to the power of -8,4 becomes possible.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 9, 2020, 1:15pm UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/26 "2020-07-09T13:15:05Z")

</div>

Hello,

My exploration of this… sharing.

> **[Exponentiation | Powers via logarithms](https://en.wikipedia.org/wiki/Exponentiation#Powers_via_logarithms)**
>
> When ex is defined as the exponential function, bx can be defined, for other positive real numbers b, in terms of ex. Specifically, the natural logarithm ln(x) is the inverse of the exponential function ex. It is defined for b \> 0, and satisfies

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c0ea5588665839b5a2a4582ca10219908fd678fa.png)

Using Processing functions and Java Math class and comparing results:

> **Code \< Expand this to see code.**
>
> ```auto
> // Exponential Powers Exploration
> // v1.0.0
> // GLV 2020-MM-DD
> 
> //https://en.wikipedia.org/wiki/Exponentiation#Powers_via_logarithms
> 
> float b = 2.0;
> float x = 3.5;  
> 
> //Processing functions
> 
> float c;
> 
> c = exp(x*log(b));
> println(c);
> 
> c = pow(b, x);
> println(c);
> 
> println();
> 
> //Java Math class 
> 
> double d;
> 
> d = Math.exp(x*Math.log(b));
> println(d);
> 
> d = Math.pow(b, x);
> println(d);
>   
> 
> ```

Processing references:  
[https://processing.org/reference/log\_.html](https://processing.org/reference/log_.html)  
[https://processing.org/reference/exp\_.html](https://processing.org/reference/exp_.html)  
[https://processing.org/reference/pow\_.html](https://processing.org/reference/pow_.html)

Processing down casts to a float (loss of precision):  
[float exp(float n)](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4523)  
[float pow(float n, float e)](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4541)  
It takes a few seconds with my browser to go to line number for links above.

`:)`

Java references:  
[Java 8 Math class](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html)  
[Source code for pow()](http://hg.openjdk.java.net/jdk8u/jdk8u40/jdk/file/c7bbaa04eaa8/src/share/native/java/lang/fdlibm/src/e_pow.c) There may be a better link to source.

`:)`

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [July 11, 2020, 7:16pm UTC](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257/27 "2020-07-11T19:16:31Z")

</div>

Thanks for the answers. 😃

[Previous page](https://discourse.processing.org/t/how-to-cube-a-sine-cosine-function-in-processing/17257.md?page=1)
