Opened 10 years ago

Closed 7 years ago

#12600 closed enhancement (fixed)

machine epsilon for RR, RDF, ...

Reported by: dkrenn Owned by: jason, jkantor
Priority: major Milestone: sage-6.4
Component: numerical Keywords: machine epsilon
Cc: Merged in:
Authors: Jeroen Demeyer Reviewers: Marc Mezzarobba
Report Upstream: N/A Work issues:
Branch: f1a086a (Commits, GitHub, GitLab) Commit: f1a086ae03df83c5277345c7b037fdfae44ae8bc
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

Matlab and Octave have a function eps(...) to show the machine epsilon. From the manual of octave: "More precisely, `eps' is the relative spacing between any two adjacent numbers in the machine's floating point system.".

Sage should have that, too.

E.g., we should have something like

sage: RDF.epsilon()
2.220446049250313e-16

Change History (14)

comment:1 follow-up: Changed 10 years ago by poeschko

IMHO this is exactly what ulp() provides. From the docs:

Returns the unit of least precision of self, which is the weight of the least significant bit of self. Unless self is exactly a power of two, it is gap between this number and the next closest distinct number that can be represented.

It gives the following:

sage: RR(1).ulp()
2.22044604925031e-16
sage: RDF(1).ulp()
1.11022302463e-16

Of course, ulp is unbounded:

sage: RDF(10**100).ulp()
9.71334446113e+83
sage: RDF(10**-100).ulp()
6.34485459329e-117

The only thing that I can imagine adding is a class method eps() to RR and RDF that returns one().ulp().

comment:2 in reply to: ↑ 1 Changed 10 years ago by dkrenn

Replying to poeschko:

IMHO this is exactly what ulp() provides. From the docs:

Thanks for mentioning, I didn't know about ulp

The only thing that I can imagine adding is a class method eps() to RR and RDF that returns one().ulp().

Yes, that would be good to have. (Note that RR is RealField(53), i.e. RealField has to be equipped with such a method.)

comment:3 Changed 9 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:4 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:6 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:7 Changed 7 years ago by jdemeyer

  • Branch set to u/jdemeyer/ticket/12600
  • Modified changed from 08/10/14 16:51:03 to 08/10/14 16:51:03

comment:8 Changed 7 years ago by jdemeyer

  • Authors set to Jeroen Demeyer
  • Commit set to 5e83b95e5161797bddc676156e9d629d839e3670
  • Status changed from new to needs_review

New commits:

5e83b95Add epsilon() method to rings

comment:9 Changed 7 years ago by jdemeyer

  • Description modified (diff)

comment:10 Changed 7 years ago by mmezzarobba

  • Status changed from needs_review to needs_work

This breaks the usual doctest in coercion_and_categories.rst.

Failed example:
    [p for p in dir(Field) if p not in dir(Parent)]
Expected:
    ['__div__', '__fraction_field', '__ideal_monoid', '__iter__',
     '__pow__', '__rdiv__', '__rpow__', '__rxor__', '__xor__',
     '_an_element', '_an_element_c', '_an_element_impl', '_coerce_',
     '_coerce_c', '_coerce_impl', '_coerce_self', '_coerce_try',
     '_default_category', '_gens', '_gens_dict',
     '_has_coerce_map_from', '_ideal_class_', '_latex_names', '_list',
     '_one_element', '_pseudo_fraction_field',
     '_random_nonzero_element', '_richcmp', '_unit_ideal',
     '_zero_element', '_zero_ideal', 'algebraic_closure',
     'base_extend', 'cardinality', 'class_group', 'coerce_map_from_c',
     'coerce_map_from_impl', 'content', 'divides', 'extension',
     'fraction_field', 'frobenius_endomorphism', 'gcd', 'gen', 'gens',
     'get_action_c', 'get_action_impl', 'has_coerce_map_from_c',
     'has_coerce_map_from_impl', 'ideal', 'ideal_monoid',
     'integral_closure', 'is_commutative', 'is_field', 'is_finite',
     'is_integral_domain', 'is_integrally_closed', 'is_noetherian',
     'is_prime_field', 'is_ring', 'is_subring',
     'krull_dimension', 'list', 'ngens', 'one', 'one_element',
     'order', 'prime_subfield', 'principal_ideal', 'quo', 'quotient',
     'quotient_ring', 'random_element', 'unit_ideal', 'zero',
     'zero_element', 'zero_ideal', 'zeta', 'zeta_order']
Got:
    ['__div__',
     '__fraction_field',
     '__ideal_monoid',
     '__iter__',
     '__pow__',
     '__rdiv__',
     '__rpow__',
     '__rxor__',
     '__xor__',
     '_an_element',
     '_an_element_c',
     '_an_element_impl',
     '_coerce_',
     '_coerce_c',
     '_coerce_impl',
     '_coerce_self',
     '_coerce_try',
     '_default_category',
     '_gens',
     '_gens_dict',
     '_has_coerce_map_from',
     '_ideal_class_',
     '_latex_names',
     '_list',
     '_one_element',
     '_pseudo_fraction_field',
     '_random_nonzero_element',
     '_richcmp',
     '_unit_ideal',
     '_zero_element',
     '_zero_ideal',
     'algebraic_closure',
     'base_extend',
     'cardinality',
     'class_group',
     'coerce_map_from_c',
     'coerce_map_from_impl',
     'content',
     'divides',
     ***'epsilon',***
     'extension',
     'fraction_field',
     'frobenius_endomorphism',
     'gcd',
     'gen',
     'gens',
     'get_action_c',
     'get_action_impl',
     'has_coerce_map_from_c',
     'has_coerce_map_from_impl',
     'ideal',
     'ideal_monoid',
     'integral_closure',
     'is_commutative',
     'is_field',
     'is_finite',
     'is_integral_domain',
     'is_integrally_closed',
     'is_noetherian',
     'is_prime_field',
     'is_ring',
     'is_subring',
     'krull_dimension',
     'list',
     'ngens',
     'one',
     'one_element',
     'order',
     'prime_subfield',
     'principal_ideal',
     'quo',
     'quotient',
     'quotient_ring',
     'random_element',
     'unit_ideal',
     'zero',
     'zero_element',
     'zero_ideal',
     'zeta',
     'zeta_order']

comment:11 Changed 7 years ago by git

  • Commit changed from 5e83b95e5161797bddc676156e9d629d839e3670 to f1a086ae03df83c5277345c7b037fdfae44ae8bc

Branch pushed to git repo; I updated commit sha1. New commits:

f1a086aFix doctest

comment:12 Changed 7 years ago by jdemeyer

  • Status changed from needs_work to needs_review

comment:13 Changed 7 years ago by mmezzarobba

  • Reviewers set to Marc Mezzarobba
  • Status changed from needs_review to positive_review

LGTM. You might want to crosslink the doc with that of RealNumber.epsilon though.

comment:14 Changed 7 years ago by vbraun

  • Branch changed from u/jdemeyer/ticket/12600 to f1a086ae03df83c5277345c7b037fdfae44ae8bc
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.