From: Axel Friedrich Date: 2012-12-11T17:22:40+09:00 Subject: Re: Binary Fixed-Point in Ruby? (Improved FixedPt?) Hi Brian, many thanks for your answer.! >> fi = FixedPt.new(0.1,5,7) > > What are the 5 and 7 meant to signify? This means: * Use 5 Bits, and * The binary point (similar to the "decimal point") is assumed to be between the 7th and 8th bit, counting from right. In the above example, it is like this: A value of decimal 0.1 shall be handled by hardware. The hardware (FPGA, actually) handels these bits (representing not exactly decimal 0.1): 01100 (5 Bits) The logic is designed in such a way, that these bits represent the following value: 0.0001100 (binary point between the 7th and 8th bit from right) > class FixBin > def initialize(value, binbits) > @value = (value * (1 << binbits)).to_i > @binbits = binbits > end > def to_f > @value * 1.0 / (1 << @binbits) > end > def to_s > res = "%0#{@binbits+1}b" % @value > res[-@binbits,0] = "." > res > end > end That's a very neat and elegant way! Thank you for that. I think, I'll modify the existing "fixedpt.rb" file for my needs, with your code in mind. Axel -- Posted via http://www.ruby-forum.com/.