Home > Software engineering >  Subtraction broken in Visual Studio Code (always returns 10)
Subtraction broken in Visual Studio Code (always returns 10)

Time:10-04

I am new to both Julia and Visual Studio.

whenever I try to subtract two numbers i.e. 3 - 5 I get 10

I am however able to add a negative number

3 - 5 gives -2 as expected

The behavior also occurs with the .- and . operators. I thought there might be something wrong with my "-" character so I tried copying and pasting from the julia page on mathematical operators (enter image description here

I was unable to find any one else with the same problem. The problem appears to be limited to Visual studio as subtraction still works fine when I run Julia in terminal.

I am using Visual Studio Code 1.71.2 with the julialang 1.7.12 extension and Julia 1.8.1

EDIT-1: If I copy and paste the code from Visual Studio Code into the julia terminal (not terminal in VSC) it works. However, if I run the code using julia the-script.jl in the terminal I still get 10 for any subtractions performed. I have also tested editing the script with nano and then running it and I am encountering the same problem.

EDIT3: As was pointed out by several responders the issue was not with VS code

enter image description here

Thank you for your help

Could someone explain where my code is breaking the function -? I am unable to reliably break it so that I get 10 whenever I try and subtract, but I have been able to reproduce the error with the above script

# #make the neutral model that I have in R in julia and benchmark both models
# #imports
using Random



# #set the seed 
Random.seed!(125)




#set the initial arguments
num_sp_local = 10::Int64;
init_sp_abundance = 100::Int64;
mig_prob = 0.01::Float64;
#meta community is uniform
num_sp-meta = 10; #ERROR IS HERE - function redefined to be 10


#the number of time steps to run the sim for
time_steps = 100000;

for i = 1:50
    println(i - 35)
end

The error can be seen above num_sp-meta = 10; redefines the - function to be 10 irrespective of what is being subtracted.

CodePudding user response:

This has nothing to do with VS Code, changing your editor/IDE is a pointless waste of time.

As @JoachimSauer suggested, someone (maybe you?) has re-defined the - function in your Julia instance.

  • Try fixing - by resetting it to Base.- like this: -(x::Int, y::Int) = Base.:-(x, y)
  • That might not work, depending on how the change was originally done.
  • Restart the REPL in VS Code. Perhaps the redefinition happened inside the script. Try to run 2 - 3, without running the-script first. This should work now.
  • This means that the change happened inside the-script.jl. Look for redefinitions of - inside that file.

If the above still doesn't work, the problem lies in some other library or script which is run. If so, try starting Julia like this:

 julia --startup-file=no

and then run 2 - 3 or something like that.

  • Related