fixnum

How to convince Lisp SBCL to do inline fixnum arithmetic?

為{幸葍}努か 提交于 2021-02-08 05:02:03
问题 I've found some techniques in other SO answers, but apparently I've been unable to convince SBCL to do inline fixnum arithmetic: (declaim (optimize (speed 2) (safety 1))) (declaim (ftype (function (fixnum fixnum) double-float) fixnumtest) (inline fixnumtest)) (defun fixnumtest (i j) (declare (type fixnum i j)) (let* ((n (the fixnum (+ i j))) (n+1 (the fixnum (1+ n)))) (declare (type fixnum n n+1)) (/ 1.0d0 (the fixnum (* n n+1)) ) ) ) (defun main () (format t "~11,9F~%" (fixnumtest 2 3)) )

How to convince Lisp SBCL to do inline fixnum arithmetic?

℡╲_俬逩灬. 提交于 2021-02-08 05:01:36
问题 I've found some techniques in other SO answers, but apparently I've been unable to convince SBCL to do inline fixnum arithmetic: (declaim (optimize (speed 2) (safety 1))) (declaim (ftype (function (fixnum fixnum) double-float) fixnumtest) (inline fixnumtest)) (defun fixnumtest (i j) (declare (type fixnum i j)) (let* ((n (the fixnum (+ i j))) (n+1 (the fixnum (1+ n)))) (declare (type fixnum n n+1)) (/ 1.0d0 (the fixnum (* n n+1)) ) ) ) (defun main () (format t "~11,9F~%" (fixnumtest 2 3)) )

Ruby: Private method called for 3:Fixnum

半腔热情 提交于 2019-12-12 22:10:29
问题 I am trying to learn the nuances of this simple function but am not sure what I need to do to fix this NoMethodError. How do I make 'split' public rather than private? Will that fix the problem? Here is my code: DATA = [3, 4, 5, 6, 7, 8] DATA.each do |line| vals = line.split print vals[0] + vals[1], " " end Here is the error message I get when I run this in IRB: NoMethodError: private method `split' called for 3:Fixnum 回答1: You are calling the method split on a number object — this method

ERROR: nil can't be coerced into Fixnum

我怕爱的太早我们不能终老 提交于 2019-12-12 01:38:36
问题 First of all, this question is slightly similar to my previous question, but I felt is different enough for me to start a new thread. The problem arises in when I try to test a validation on my model. I have a User model that must require the field :default_price. My test is as follows: it "should require default packs" do User.new(FactoryGirl.build(:user, :default_packs => " ")).should_not be_valid end However, when I run the test, I get the following error: Failure/Error: should_not be

Ruby no implicit conversion of Fixnum into String (TypeError)

妖精的绣舞 提交于 2019-12-09 08:21:38
问题 I am trying to answer the following question from Chris Pine's "Learn to Program" book: Leap years. Write a program that asks for a starting year and an ending year and then puts all the leap years between them (and including them, if they are also leap years). Leap years are years divisible by 4 (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are also divisible by 400 (such as 1600 and 2000, which were in fact leap years). What a

How do I determine the length of a Fixnum in Ruby?

只谈情不闲聊 提交于 2019-12-05 22:15:07
问题 In the script I'm writing, I want find the length of a Fixnum in Ruby. I could do <num>.to_s.length , but is there any way to directly find the length of a Fixnum without converting it into a String? 回答1: puts Math.log10(1234).to_i + 1 # => 4 You could add it to Fixnum like this: class Fixnum def num_digits Math.log10(self).to_i + 1 end end puts 1234.num_digits # => 4 回答2: Ruby 2.4 has an Integer#digits method, which return an Array containing the digits. num = 123456 num.digits # => [6, 5, 4

How do I determine the length of a Fixnum in Ruby?

帅比萌擦擦* 提交于 2019-12-04 03:27:30
In the script I'm writing, I want find the length of a Fixnum in Ruby. I could do <num>.to_s.length , but is there any way to directly find the length of a Fixnum without converting it into a String? puts Math.log10(1234).to_i + 1 # => 4 You could add it to Fixnum like this: class Fixnum def num_digits Math.log10(self).to_i + 1 end end puts 1234.num_digits # => 4 Ruby 2.4 has an Integer#digits method, which return an Array containing the digits. num = 123456 num.digits # => [6, 5, 4, 3, 2, 1] num.digits.count # => 6 EDIT: To handle negative numbers (thanks @MatzFan), use the absolute value.

Ruby no implicit conversion of Fixnum into String (TypeError)

纵饮孤独 提交于 2019-12-03 10:46:56
I am trying to answer the following question from Chris Pine's "Learn to Program" book: Leap years. Write a program that asks for a starting year and an ending year and then puts all the leap years between them (and including them, if they are also leap years). Leap years are years divisible by 4 (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are also divisible by 400 (such as 1600 and 2000, which were in fact leap years). What a mess! I get the following error when I run my code: leap_year.rb:12:in +': no implicit conversion of

Turning long fixed number to array Ruby

淺唱寂寞╮ 提交于 2019-11-27 16:05:19
Is there a method in ruby to turn fixnum like 74239 into an array like [7,4,2,3,9] ? You don't need to take a round trip through string-land for this sort of thing: def digits(n) Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 } end ary = digits(74239) # [7, 4, 2, 3, 9] This does assume that n is positive of course, slipping an n = n.abs into the mix can take care of that if needed. If you need to cover non-positive values, then: def digits(n) return [0] if(n == 0) if(n < 0) neg = true n = n.abs end a = Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 } a[0] *= -1 if(neg) a

Ruby max integer

六眼飞鱼酱① 提交于 2019-11-27 03:02:57
I need to be able to determine a systems maximum integer in Ruby. Anybody know how, or if it's possible? Matthew Crumley Ruby automatically converts integers to a large integer class when they overflow, so there's (practically) no limit to how big they can be. If you are looking for the machine's size, i.e. 64- or 32-bit, I found this trick at ruby-forum.com : machine_bytes = ['foo'].pack('p').size machine_bits = machine_bytes * 8 machine_max_signed = 2**(machine_bits-1) - 1 machine_max_unsigned = 2**machine_bits - 1 If you are looking for the size of Fixnum objects (integers small enough to