Pregunta

I'm trying to solve a 'decaying' puzzle that goes somewhat like this:

given A is 100 at DateTime.new(2012,5,10,0,0,0) and is decaying by 0.5 every 12 seconds, has it decayed exactly 20 by DateTime.new(2012,5,10,0,8,0)?

It so happens that the answer to that question is - well, true :)

But what about

  • A being 1304.5673,
  • the decay 0.00000197 every 1.2 msec
  • and end time being not one but 2000 DateTime.new's

I've tried with

fd=3.minutes.ago.to_datetime
td=Time.now
material=1304.5673
decay=0.00000197
step=0.00012.seconds
fd.step(td,step){ |n| material-=decay }
puts material

and the processing time is acceptable - but if I step any further back in time (like perhaps 10.hours or even 2.hours; my CPU cooler starts building up momentum, like it was about to propel the entire Mac into orbit :(

¿Fue útil?

Solución

I've toiled with this problem for quite a while - even though the timespan from question to answer on SO does indicate the opposite <:)

(and the answer, to me, explicitly demonstrates why Ruby is such a wonderful language!)

# recap the variables in the question
total_decay = ((td.to_time - fd.to_time).divmod( step))[0]* decay
puts "new material: #{material - total_decay}"

The results will probably not pass scientific scrutiny, but I'm OK with that (for now) ;)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top