samof76

samof76

Simple multiplcation function giving wrong for negative computation

Hi I have written the following program math.ex

defmodule Math do
  def mul(_, 0) do
    0
  end
  def mul(0, _) do
    0
  end

  def mul(a, b) when b < 0 do
    sum = a + mul(a, b+1)
    sum*(-1)
  end

  def mul(a, b) do
    sum = a + mul(a, b-1)
    sum
  end
end

Now, when it try to the do Math.mul(4,-10) the expected answer is the -40 but it gives me 0. Also if i change the line 7 to just return sum instead of sum*(-1) I get 40.

First Post!

ityonemo

ityonemo

those results very much seem correct! I suggest unrolling your math carefully. Maybe do Math.mul(4, -2) by hand.

Most Liked

NobbZ

NobbZ

mul(a, -1) returns -a, mul(a, -2) returns -(a + mul(a, -1)) which is -(a - a) which is -a + a, which is 0.

So on an even second argument you’ll always get 0 and on odds you get -a.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement