# Fesetround in njit function

**URL:** <https://numba.discourse.group/t/fesetround-in-njit-function/2023>\
**Category:** Community Support\
**Created:** [July 22, 2023, 10:14am UTC](https://numba.discourse.group/t/fesetround-in-njit-function/2023 "2023-07-22T10:14:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosckon](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/biosckon/32/775_2.png) [@biosckon](https://numba.discourse.group/u/biosckon)\
**Post date:** [July 22, 2023, 10:14am UTC](https://numba.discourse.group/t/fesetround-in-njit-function/2023/1 "2023-07-22T10:14:10Z")

</div>

I’m playing with Interval Arithmetic (e.g. see Validated Numerics by Tucker).

Naturally wanted to use numba for speed.

Vanilla Python function does what **expected**. First print:

`(0.09999999999999999, 0.1)`

second print with numba.njit produces something **unexpected** :

`(0.09999999999999999, 0.09999999999999999)`

adding mathematically insignificant `+ 1 - 1` restores expected behavior. (see comment in code)

It looks like somewhere in the numba/llvm belly repeating `x/y’ gets optimized away into a single computation.

Which for my purpose breaks intended sequence of execution. Is there a way to control this sort of optimization in numba.njit?

Thanks!

```python
from numba import njit
from ctypes import cdll, c_int

fesetround = cdll.ucrtbased.fesetround
fesetround.restype = c_int
fesetround.argtypes = [c_int]

FE_DOWN = 0x0100
FE_UP = 0x0200
FE_NEAR = 0x0

def test_div(x, y):
    fesetround(FE_DOWN)
    a = x/ y
    fesetround(FE_UP)

    # un-comment to get expected behavior
    b = x/ y # + 1 - 1

    fesetround(FE_NEAR)
    return a, b

print("py:", test_div(1, 10))
print("nb:", njit(test_div)(1, 10))

```

---

<div class="post-metadata">

**Author:** ![biosckon](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/biosckon/32/775_2.png) [@biosckon](https://numba.discourse.group/u/biosckon)\
**Post date:** [July 22, 2023, 10:28am UTC](https://numba.discourse.group/t/fesetround-in-njit-function/2023/2 "2023-07-22T10:28:14Z")

</div>

Been playing with equivalent C:

```auto
#include <fenv.h>
#include <stdio.h>

int main() {
  double x = 1, y = 10;

  fesetround(FE_UPWARD);
  double a = x / y;
  fesetround(FE_DOWNWARD);
  double b = x / y;

  printf("%g %g\n", a, b);
  return 0;
}

```

compiling with `-O0` produces **expected**  
`clang main.c -o main -lm -Wall -g -O0`  
`./main`  
`0.1 0.0999999`

compiling with `-O1` produces **un-expected** or rather **un-desired** :  
`clang main.c -o main -lm -Wall -g -O1`  
`./main`  
`0.1 0.1`

In C it does appear to be some sort of optimization induced result. Oh and adding `+1-1` does not fool the c compiler.
