# Why Julia is giving a different answer than r?

**URL:** https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918
**Category:** New to Julia
**Tags:** question, broadcasting
**Created:** [May 16, 2023, 8:48am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918 "2023-05-16T08:48:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [May 16, 2023, 8:48am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/1 "2023-05-16T08:48:59Z")

</div>

i have a simple equation as given below (julia code). I’m getting different answer as compare to r. Am I missing something?

```julia
x=100
y=0
d = 20
h = -2
c = [0, 1, 2, 3, 4, 5]
A = x .+ ((y - x) / (1 .+ (c / d).^h))

```

answer in r : 100.00000 99.75062 99.00990 97.79951 96.15385 94.11765  
answer in julia: NaN 100.0 100.0 100.0 100.0 100.0

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 16, 2023, 8:51am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/2 "2023-05-16T08:51:50Z")

</div>

The code that you’ve presented isn’t runnable. I’ve fixed it here, and the answer seems to match that from R:

```julia
julia> x=100;

julia> y=0;

julia> d = 20;

julia> h = -2;

julia> c = [0, 1, 2, 3, 4, 5];

julia> A = x .+ ((y - x) ./ (1 .+ (c / d).^h))
6-element Vector{Float64}:
 100.0
  99.75062344139651
  99.00990099009901
  97.79951100244499
  96.15384615384616
  94.11764705882354

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [May 16, 2023, 8:53am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/3 "2023-05-16T08:53:00Z")

</div>

I’d suggest using `@.` It makes it easier to avoid excess allocations, e.g. `c / d` should be `c ./ d`.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 16, 2023, 8:54am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/4 "2023-05-16T08:54:25Z")

</div>

I agree, this is often better:

```julia
julia> A = @. x + ((y - x) / (1 + (c / d)^h))
6-element Vector{Float64}:
 100.0
  99.75062344139651
  99.00990099009901
  97.79951100244499
  96.15384615384616
  94.11764705882354

```

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [May 16, 2023, 9:00am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/5 "2023-05-16T09:00:15Z")

</div>

Thanks a lot for help and suggestions. 🙂

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [May 16, 2023, 9:30am UTC](https://discourse.julialang.org/t/why-julia-is-giving-a-different-answer-than-r/98918/6 "2023-05-16T09:30:19Z")

</div>

> [@jishnub](#):
>
> The code that you’ve presented isn’t runnable.

The original code with the missing dot runs on Julia 1.8 but the `number/vector` method was removed in 1.9: [Remove number / vector by antoine-levitt · Pull Request #44358 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/44358) precisely to avoid this type of mistake 🙂
