Division operator need float conversion before

Hi,

Just to tell that the / operator can lead to confusion with kids :slight_smile:
We have to convert before as a float.
Yes it’s correct as sonic pi uses ruby language but for kids it can be a bit tricky to explain.

use_midi_defaults port: "microsoft_gs_wavetable_synth_0"
use_bpm 60

# boucles

puts 1/2 # gives 0
puts 1.to_f / 2 # gives 0.5

4.times do
  midi :c2,channel: 10
  midi :fs2,channel: 10
  sleep 0.5
  midi :fs2,channel: 10
  sleep (1 / 2) # hum 1 divided by 2 equals 0.5 no ? No it returns 0 as an integer
  sleep (1.to_f / 2) # 
  
  midi :d2,channel: 10
  midi :fs2,channel: 10
  sleep 0.5
  midi :fs2,channel: 10
  sleep 0.5
end

midi :cs3,channel: 10

Agreed that it’s confusing. Unfortunately there’s nothing we can do about it unless we create our own fork of Ruby and start making changes. This isn’t something I’m prepared to do.

Very surprised you did no want to fork the ruby language :slight_smile:
By the way; it’s a good occasion to explain that in code there are different types of variables right ?

This one gets me once in while and I’ve been coding for years.

Just a heads up, you don’t need to use 1.to_f / 2, you can just do 1.0 / 2 in stead of 1 / 2. By adding the .0 you make it a float.

oh yes ! i have forgot this tip ! thanks for the reminder