# Problems with fractions

**URL:** <https://discourse.processing.org/t/problems-with-fractions/29605>\
**Category:** Beginners\
**Created:** [April 24, 2021, 4:10pm UTC](https://discourse.processing.org/t/problems-with-fractions/29605 "2021-04-24T16:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [April 24, 2021, 4:10pm UTC](https://discourse.processing.org/t/problems-with-fractions/29605/1 "2021-04-24T16:10:00Z")

</div>

Hi, I am trying to make a program where i need to calculate a fraction (3-2)/2 or just 1/2 which is 0,5 ofcourse, however processing says it is 0. I don’t know if it’s a bug or what. I would just like to know how i can fix the problem 🙂 .  
For exampel if i write something like:

```auto
float x=1/2
println(x)

```

it just prints 0

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 24, 2021, 4:17pm UTC](https://discourse.processing.org/t/problems-with-fractions/29605/2 "2021-04-24T16:17:01Z")

</div>

> [@Unable to generate proper float value in Processing](https://discourse.processing.org/t/unable-to-generate-proper-float-value-in-processing/29352/2):
>
> At least 1 of the variables i or n needs to be of datatype float for the operator / to evaluate its operands as a float value. face_with_monocle Alternatively you can temporarily upcast 1 of the variables to float: float k = kMax \* sqrt( (float) i / n );

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [April 24, 2021, 5:06pm UTC](https://discourse.processing.org/t/problems-with-fractions/29605/3 "2021-04-24T17:06:20Z")

</div>

I f you want to do “proper” rational arithmetic, I think ApacheMath supports it for java. Some languages like ruby have support out of the box:-

```ruby
half = Rational(1, 2)
puts half.to_s
calculated = Rational(2 - 1, 2)
puts calculated.to_s
quarter = Rational(1, 4)
puts (quarter + half).to_s

```

Output

```bash
1/2
1/2
3/4

```

The problem with float arithmetic there are always errors, which are worse for small numbers…

In your case you probably want

```java

float x = 1 / 2.0 // that forces float answer from processing (or double answer if using regular java)

```

And accept the errors for now.
