[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

SWIG function problem

flashdog

12/16/2007 9:54:00 AM

Hello,
I have following Ruby code:
class Random
def initialize(init, im=139968, ia=3877, ic=29573)
@last = init
@im = im
@ia = ia
@ic = ic
end
def next(max)
(max * (@last = (@last * @ia + @ic) % @im)) / @im
end
end

v = Random.new(42.0)
10.times do
puts v.next(1.0)
end

And I try to write the next-function in c
float gen_random(int *last, float max, int IM, int IA, int IC) {
*last = (*last * IA + IC) % IM;
return (max * *last / (float)IM);
}
and with help SWIG I try make useable in Ruby but I can not.

require 'fastac'
...
def next(max)
# (max * (@last = (@last * @ia + @ic) % @im)) / @im
Fastac.gen_random(@last, max, @im, @ia, @ic)
end
...


$ ruby gen_random.rb
gen_random.rb:12:in `gen_random': Expected argument 0 of type int *, but
got Fixnum 42 (TypeError)
in SWIG method 'gen_random' from gen_random.rb:12:in `next'
from gen_random.rb:18
from gen_random.rb:17:in `times'
from gen_random.rb:17
[/code]

What I make wrong?

best regards
--
Posted via http://www.ruby-....

2 Answers

Jano Svitok

12/17/2007 4:35:00 PM

0

On Dec 16, 2007 10:53 AM, Fla As <flashdog@gmx.net> wrote:
> And I try to write the next-function in c
> float gen_random(int *last, float max, int IM, int IA, int IC) {
> *last = (*last * IA + IC) % IM;
> return (max * *last / (float)IM);
> }
> and with help SWIG I try make useable in Ruby but I can not.

See http://www.swig.org/Doc1.3/Ruby.html...
(you can reuse your original header if you #ifdef swig directives, see
swig doc for more info).

If that doesn't help, please post somewhere your code, including ruby,
c, and swig header. It would make reproducing your problem easier.

J.

flashdog

12/20/2007 2:37:00 PM

0

Your link doesn't help. My little code is atteched.

Can you please remove my email address from your last message?


Jano Svitok wrote:
>> And I try to write the next-function in c
>> float gen_random(int *last, float max, int IM, int IA, int IC) {
>> *last = (*last * IA + IC) % IM;
>> return (max * *last / (float)IM);
>> }
>> and with help SWIG I try make useable in Ruby but I can not.
>
> See http://www.swig.org/Doc1.3/Ruby.html...
> (you can reuse your original header if you #ifdef swig directives, see
> swig doc for more info).
>
> If that doesn't help, please post somewhere your code, including ruby,
> c, and swig header. It would make reproducing your problem easier.
>
> J.


Attachments:
http://www.ruby-...attachment/1183/sw...

--
Posted via http://www.ruby-....