| Class | XSD::XSDFloat |
| In: |
lib/xsd/datatypes.rb
|
| Parent: | XSDAnySimpleType |
| Type | = | QName.new(Namespace, FloatLiteral) |
# File lib/xsd/datatypes.rb, line 306
306: def initialize(value = nil)
307: init(Type, value)
308: end
# File lib/xsd/datatypes.rb, line 362
362: def self.positive?(value)
363: (1 / value) > 0.0
364: end
# File lib/xsd/datatypes.rb, line 338
338: def _to_s
339: if @data.nan?
340: 'NaN'
341: elsif @data.infinite? == 1
342: 'INF'
343: elsif @data.infinite? == -1
344: '-INF'
345: else
346: sign = XSDFloat.positive?(@data) ? '+' : '-'
347: sign + sprintf("%.10g", @data.abs).sub(/[eE]([+-])?0+/) { 'e' + $1 }
348: end
349: end
Convert to single-precision 32-bit floating point value.
# File lib/xsd/datatypes.rb, line 352
352: def narrow32bit(f)
353: if f.nan? || f.infinite?
354: f
355: elsif f.abs < MIN_POSITIVE_SINGLE
356: XSDFloat.positive?(f) ? POSITIVE_ZERO : NEGATIVE_ZERO
357: else
358: f
359: end
360: end
# File lib/xsd/datatypes.rb, line 312
312: def screen_data(value)
313: # "NaN".to_f => 0 in some environment. libc?
314: if value.is_a?(Float)
315: return narrow32bit(value)
316: end
317: str = value.to_s.strip
318: if str == 'NaN'
319: NaN
320: elsif str == 'INF'
321: POSITIVE_INF
322: elsif str == '-INF'
323: NEGATIVE_INF
324: else
325: if /^[+\-\.\deE]+$/ !~ str
326: raise ValueSpaceError.new("#{ type }: cannot accept '#{ str }'.")
327: end
328: # Float("-1.4E") might fail on some system.
329: str << '0' if /e$/i =~ str
330: begin
331: return narrow32bit(Float(str))
332: rescue ArgumentError
333: raise ValueSpaceError.new("#{ type }: cannot accept '#{ str }'.")
334: end
335: end
336: end