@@ -222,59 +222,99 @@ def to_f() 1.2 end
222
222
end
223
223
end
224
224
225
- describe "for hexadecimal literals with binary exponent" do
226
- %w( p P ) . each do |p |
227
- it "interprets the fractional part (on the left side of '#{ p } ') in hexadecimal" do
228
- @object . send ( :Float , "0x10#{ p } 0" ) . should == 16.0
229
- end
230
-
231
- it "interprets the exponent (on the right of '#{ p } ') in decimal" do
232
- @object . send ( :Float , "0x1#{ p } 10" ) . should == 1024.0
233
- end
234
-
235
- it "raises an ArgumentError if #{ p } is the trailing character" do
236
- -> { @object . send ( :Float , "0x1#{ p } " ) } . should raise_error ( ArgumentError )
237
- end
238
-
239
- it "raises an ArgumentError if #{ p } is the leading character" do
240
- -> { @object . send ( :Float , "0x#{ p } 1" ) } . should raise_error ( ArgumentError )
241
- end
242
-
243
- it "returns Infinity for '0x1#{ p } 10000'" do
244
- @object . send ( :Float , "0x1#{ p } 10000" ) . should == Float ::INFINITY
245
- end
246
-
247
- it "returns 0 for '0x1#{ p } -10000'" do
248
- @object . send ( :Float , "0x1#{ p } -10000" ) . should == 0
249
- end
225
+ context "for hexadecimal literals" do
226
+ it "interprets the 0x prefix as hexadecimal" do
227
+ @object . send ( :Float , "0x10" ) . should == 16.0
228
+ @object . send ( :Float , "0x0F" ) . should == 15.0
229
+ @object . send ( :Float , "0x0f" ) . should == 15.0
230
+ end
250
231
251
- it "allows embedded _ in a number on either side of the #{ p } " do
252
- @object . send ( :Float , "0x1_0#{ p } 10" ) . should == 16384.0
253
- @object . send ( :Float , "0x10#{ p } 1_0" ) . should == 16384.0
254
- @object . send ( :Float , "0x1_0#{ p } 1_0" ) . should == 16384.0
255
- end
232
+ it "accepts embedded _ if the number does not contain a-f" do
233
+ @object . send ( :Float , "0x1_0" ) . should == 16.0
234
+ end
256
235
257
- it "raises an exception if a space is embedded on either side of the '#{ p } '" do
258
- -> { @object . send ( :Float , "0x1 0#{ p } 10" ) } . should raise_error ( ArgumentError )
259
- -> { @object . send ( :Float , "0x10#{ p } 1 0" ) } . should raise_error ( ArgumentError )
236
+ ruby_version_is "" ..."3.4.3" do
237
+ it "does not accept embedded _ if the number contains a-f" do
238
+ -> { @object . send ( :Float , "0x1_0a" ) } . should raise_error ( ArgumentError )
239
+ @object . send ( :Float , "0x1_0a" , exception : false ) . should be_nil
260
240
end
241
+ end
261
242
262
- it "raises an exception if there's a leading _ on either side of the ' #{ p } ' " do
263
- -> { @object . send ( :Float , "0x_10 #{ p } 10" ) } . should raise_error ( ArgumentError )
264
- -> { @object . send ( :Float , "0x10 #{ p } _10" ) } . should raise_error ( ArgumentError )
243
+ ruby_version_is "3.4.3 " do
244
+ it "accepts embedded _ if the number contains a-f" do
245
+ @object . send ( :Float , "0x1_0a" ) . should == 0x10a . to_f
265
246
end
247
+ end
266
248
267
- it "raises an exception if there's a trailing _ on either side of the '#{ p } '" do
268
- -> { @object . send ( :Float , "0x10_#{ p } 10" ) } . should raise_error ( ArgumentError )
269
- -> { @object . send ( :Float , "0x10#{ p } 10_" ) } . should raise_error ( ArgumentError )
270
- end
249
+ it "does not accept _ before, after or inside the 0x prefix" do
250
+ -> { @object . send ( :Float , "_0x10" ) } . should raise_error ( ArgumentError )
251
+ -> { @object . send ( :Float , "0_x10" ) } . should raise_error ( ArgumentError )
252
+ -> { @object . send ( :Float , "0x_10" ) } . should raise_error ( ArgumentError )
253
+ @object . send ( :Float , "_0x10" , exception : false ) . should be_nil
254
+ @object . send ( :Float , "0_x10" , exception : false ) . should be_nil
255
+ @object . send ( :Float , "0x_10" , exception : false ) . should be_nil
256
+ end
271
257
272
- it "allows hexadecimal points on the left side of the '#{ p } '" do
273
- @object . send ( :Float , "0x1.8#{ p } 0" ) . should == 1.5
258
+ ruby_version_is "3.4" do
259
+ it "accepts a fractional part" do
260
+ @object . send ( :Float , "0x0.8" ) . should == 0.5
274
261
end
262
+ end
275
263
276
- it "raises an ArgumentError if there's a decimal point on the right side of the '#{ p } '" do
277
- -> { @object . send ( :Float , "0x1#{ p } 1.0" ) } . should raise_error ( ArgumentError )
264
+ describe "with binary exponent" do
265
+ %w( p P ) . each do |p |
266
+ it "interprets the fractional part (on the left side of '#{ p } ') in hexadecimal" do
267
+ @object . send ( :Float , "0x10#{ p } 0" ) . should == 16.0
268
+ end
269
+
270
+ it "interprets the exponent (on the right of '#{ p } ') in decimal" do
271
+ @object . send ( :Float , "0x1#{ p } 10" ) . should == 1024.0
272
+ end
273
+
274
+ it "raises an ArgumentError if #{ p } is the trailing character" do
275
+ -> { @object . send ( :Float , "0x1#{ p } " ) } . should raise_error ( ArgumentError )
276
+ end
277
+
278
+ it "raises an ArgumentError if #{ p } is the leading character" do
279
+ -> { @object . send ( :Float , "0x#{ p } 1" ) } . should raise_error ( ArgumentError )
280
+ end
281
+
282
+ it "returns Infinity for '0x1#{ p } 10000'" do
283
+ @object . send ( :Float , "0x1#{ p } 10000" ) . should == Float ::INFINITY
284
+ end
285
+
286
+ it "returns 0 for '0x1#{ p } -10000'" do
287
+ @object . send ( :Float , "0x1#{ p } -10000" ) . should == 0
288
+ end
289
+
290
+ it "allows embedded _ in a number on either side of the #{ p } " do
291
+ @object . send ( :Float , "0x1_0#{ p } 10" ) . should == 16384.0
292
+ @object . send ( :Float , "0x10#{ p } 1_0" ) . should == 16384.0
293
+ @object . send ( :Float , "0x1_0#{ p } 1_0" ) . should == 16384.0
294
+ end
295
+
296
+ it "raises an exception if a space is embedded on either side of the '#{ p } '" do
297
+ -> { @object . send ( :Float , "0x1 0#{ p } 10" ) } . should raise_error ( ArgumentError )
298
+ -> { @object . send ( :Float , "0x10#{ p } 1 0" ) } . should raise_error ( ArgumentError )
299
+ end
300
+
301
+ it "raises an exception if there's a leading _ on either side of the '#{ p } '" do
302
+ -> { @object . send ( :Float , "0x_10#{ p } 10" ) } . should raise_error ( ArgumentError )
303
+ -> { @object . send ( :Float , "0x10#{ p } _10" ) } . should raise_error ( ArgumentError )
304
+ end
305
+
306
+ it "raises an exception if there's a trailing _ on either side of the '#{ p } '" do
307
+ -> { @object . send ( :Float , "0x10_#{ p } 10" ) } . should raise_error ( ArgumentError )
308
+ -> { @object . send ( :Float , "0x10#{ p } 10_" ) } . should raise_error ( ArgumentError )
309
+ end
310
+
311
+ it "allows hexadecimal points on the left side of the '#{ p } '" do
312
+ @object . send ( :Float , "0x1.8#{ p } 0" ) . should == 1.5
313
+ end
314
+
315
+ it "raises an ArgumentError if there's a decimal point on the right side of the '#{ p } '" do
316
+ -> { @object . send ( :Float , "0x1#{ p } 1.0" ) } . should raise_error ( ArgumentError )
317
+ end
278
318
end
279
319
end
280
320
end
0 commit comments