File: prev_float_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (49 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require_relative '../../spec_helper'

describe "Float#prev_float" do
  it "returns a float the smallest possible step smaller than the receiver" do
    barely_negative = 0.0.prev_float
    barely_negative.should == 0.0.prev_float

    barely_negative.should < 0.0
    barely_negative.should > barely_negative.prev_float

    midpoint = barely_negative / 2
    [0.0, barely_negative].should include midpoint
  end

  it "returns -Float::INFINITY for -Float::INFINITY" do
    (-Float::INFINITY).prev_float.should == -Float::INFINITY
  end

  it "steps directly between MAX and INFINITY" do
    Float::INFINITY.prev_float.should == Float::MAX
    (-Float::MAX).prev_float.should == -Float::INFINITY
  end

  it "steps directly between 1.0 and 1.0 - EPSILON/2" do
    1.0.prev_float.should == 1.0 - Float::EPSILON/2
  end

  it "steps directly between -1.0 and -1.0 - EPSILON" do
    (-1.0).prev_float.should == -1.0 - Float::EPSILON
  end

  it "reverses the effect of next_float for all Floats except -INFINITY and -0.0" do
    num = rand
    num.next_float.prev_float.should == num
  end

  it "returns positive zero when stepping downward from just above zero" do
    x = 0.0.next_float.prev_float
    (1/x).should == Float::INFINITY
  end

  it "gives the same result for -0.0 as for +0.0" do
    (0.0).prev_float.should == (-0.0).prev_float
  end

  it "returns NAN if NAN was the receiver" do
    Float::NAN.prev_float.should.nan?
  end
end