# Geometric mean bit hack

**URL:** https://discourse.processing.org/t/geometric-mean-bit-hack/14366
**Category:** Gallery
**Created:** [October 2, 2019, 8:40am UTC](https://discourse.processing.org/t/geometric-mean-bit-hack/14366 "2019-10-02T08:40:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![seanc4s](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@seanc4s](https://discourse.processing.org/u/seanc4s)
#### Post date: [October 2, 2019, 8:40am UTC](https://discourse.processing.org/t/geometric-mean-bit-hack/14366/1 "2019-10-02T08:40:22Z")

</div>

```auto
// Bit hack fast geometric mean
void setup() {
  float m1=300f;
  background(0);
  size(500, 500);
  stroke(255, 0, 0); // red
  for (int i=0; i<500; i++) {
    point(i, 499-sqrt(m1*i));
  }
  stroke(0, 255, 0); // green
  for (int i=0; i<500; i++) {
    float f=i;
    int j=Float.floatToRawIntBits(f);
    int k=Float.floatToRawIntBits(m1);
    j=(j+k)>>>1;  
    float p=Float.intBitsToFloat(j);
    point(i, 499-p);
  }
}

```

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [October 14, 2019, 10:39pm UTC](https://discourse.processing.org/t/geometric-mean-bit-hack/14366/2 "2019-10-14T22:39:33Z")

</div>

Nice! Thanks for sharing. Your post brought me also to [this](https://github.com/keon/awesome-bits) link.

---

<div class="post-metadata">

### Author: ![seanc4s](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@seanc4s](https://discourse.processing.org/u/seanc4s)
#### Post date: [October 14, 2019, 11:21pm UTC](https://discourse.processing.org/t/geometric-mean-bit-hack/14366/3 "2019-10-14T23:21:08Z")

</div>

Thanks for the link. Floating point bit hacks are very useful for experimenting with neural network activation functions. If you just want a little bit of nonlinarity for example you can multiply the approximate square root of a number by itself.  
Or if you want to convert 2 numbers from the Gaussian probability distribution to a number from the uniform distribution you can use atan2(x,y).

> [@Fast approximate square root](https://discourse.processing.org/t/fast-approximate-square-root/12074/9):
>
> Very approximate atan2(…). I didn’t check edge cases like atan2(0,0) etc. /\*\* \* Arctangent Bit Hack Version \* \* Move the mouse to change the direction of the eyes. \* The atan2() function computes the angle from each eye \* to the cursor. \*/ Eye e1, e2, e3; void setup() { size(640, 360); noStroke(); e1 = new Eye( 250, 16, 120); e2 = new Eye( 164, 185, 80); e3 = new Eye( 420, 230, 220); } void draw() { background(102); e1.update(mouseX, mouseY); e2.update(mou…
