Error with Float.floor when running locally

I’ve done a couple of the Elixir exercises in the browser fairly easily, but I prefer the IDE I’m used to, especially the auto indenting.

I installed the exercise with the link and everything seemed fine. I ran into an issue when I ran the tests for days_in_budget. I’m getting the following error for each test it attempts


  1) test days_in_budget/3 it's the budget divided by the daily rate (FreelancerRatesTest)
     test/freelancer_rates_test.exs:66
     ** (UndefinedFunctionError) function Float.floor/3 is undefined or private. Did you mean one of:
     
           * floor/1
           * floor/2
     
     code: assert FreelancerRates.days_in_budget(1_600, 50, 0.0) == 4
     stacktrace:
       (elixir 1.12.2) Float.floor(0.022727272727272728, 0.0, 1)
       test/freelancer_rates_test.exs:67: (test)

Here is the code for my defmodule. Since I had done most of it in the browser I copied and pasted what I had already done into mix.exs. I started Elixir yesterday and I could figure out where else to put it. I browsed the docs but didn’t read them fully and attempted to google my error message, but I couldn’t find anything.

I changed my days_in_budget function in the hopes that some clarity and simplification would help.

defmodule FreelancerRates do

  def daily_rate(hourly_rate) do
    8.0 * hourly_rate
  end

  def apply_discount(before_discount, discount) do
    before_discount -(before_discount * (discount/100))
  end

  def monthly_rate(hourly_rate, discount) do
    trunc(Float.ceil(apply_discount(daily_rate(hourly_rate) * 22, discount)))
  end

  def days_in_budget(budget, hourly_rate, discount) do
    daily = daily_rate(hourly_rate)
    discounted = apply_discount(daily, discount)
    total = budget/discounted
    Float.floor(total, 1)
  end
end

The exact same code passed fine in the browser so this is something I’m missing or don’t understand about how Elixir works outside of a single file.

Thanks

It looks to me like the original version of your days_in_budget function must have been calling Float.floor(total, discount, 1) instead of Float.floor(total, 1). The error you saw is telling you that there is no version of Float.floor that accept 3 arguments, only 1 or 2. Hope that helps.

I’ll check tomorrow when I’m back on my computer. Thanks. :slightly_smiling_face:

The errors are indicating that there are three arguments being passed to Float.floor . The errors are a result of the code

  def days_in_budget(budget, hourly_rate, discount) do
    daily = daily_rate(hourly_rate)
    discounted = apply_discount(daily, discount)
    total = budget/discounted
    Float.floor(total, 1)
  end

At one point I was accidently putting three arguments do to the deeply nested function calls. So I split them into their own variables so I could ensure I was only passing two arguments. I’ve being getting

 * test days_in_budget/3 it's the budget divided by th  * test days_in_budget/3 it's the budget divided by the daily rate (2.2ms) [L#66]

  1) test days_in_budget/3 it's the budget divided by the daily rate (FreelancerRatesTest)
     test/freelancer_rates_test.exs:66
     ** (UndefinedFunctionError) function Float.floor/3 is undefined or private. Did you mean one of:
     
           * floor/1
           * floor/2
     
     code: assert FreelancerRates.days_in_budget(1_600, 50, 0.0) == 4
     stacktrace:
       (elixir 1.12.2) Float.floor(0.022727272727272728, 0.0, 1)
       test/freelancer_rates_test.exs:67: (test)

With the code that only has 2 arguments that I can see.

I can’t see how your solution, which is correct, could cause an error about Float.floor/3 being an unknown function. The solution is clearly calling Float.floor/2, not Float.floor/3. So we have to assume that the code that you have written most recently is not the code that is being executed :thinking:

Are you using the mix test command to run the tests from inside the elixir/freelancer-rates directory? Is your solution inside of the elixir/freelancer-rates/lib/freelancer_rates.ex file? Are there any other files in the directory elixir/freelancer-rates/lib/?

Can you run remove the directory elixir/freelancer-rates/_build/ and run the tests again?

My solution is in the mix.exs. I’ve never worked with Elixir before and it wasn’t clear when I should put my code.

I did a fresh install of vscode this morning because it was using almost all of my memory when I opened it and it’s working now even though my code is in the mix.exs file. It is also magically in the lib/freelancer_rates.ex. :woman_shrugging:

I must’ve had some plugin somewhere interfering.

Thanks for pointing out where my code should have gone @angelikatyborska