Code Monkey home page Code Monkey logo

Comments (9)

BebeSparkelSparkel avatar BebeSparkelSparkel commented on August 17, 2024

@tjhladish Hi Tom.
Thank you so much for finding this!
I really appreciate you providing a solution as well. In the commit listed above I tweaked your solution just a bit and added tests for the problems that you outlined. Could you please take a look at what I have done to ensure that your problem is really fixed?

from to-precision.

BebeSparkelSparkel avatar BebeSparkelSparkel commented on August 17, 2024

@tjhladish Have you had time to look at this yet?

from to-precision.

tjhladish avatar tjhladish commented on August 17, 2024

@BebeSparkelSparkel Sorry for the delay, William. This doesn't seem to fix the special case--in fact the answers I'm getting are now sometimes mathematically incorrect:

>>> from to_precision import *
>>> std_notation(0.989999, 3)
'0.990' # good
>>> std_notation(0.999999, 3)
'10.00' # 10x greater than correct answer, extra sig fig
>>> std_notation(1.999999, 3)
'2.00' # good
>>> std_notation(0.0999999, 3)
'1.000' # same problem as above

From a cursory check, it looks like the other notation types reflect the same issue.

Best,
Tom

from to-precision.

BebeSparkelSparkel avatar BebeSparkelSparkel commented on August 17, 2024

I made an error refactoring.
I have also added travis-ci to ensure things are working correctly.
Can you please check if it is working correctly now?

from to-precision.

BebeSparkelSparkel avatar BebeSparkelSparkel commented on August 17, 2024

Everything seems to be passing now.

from to-precision.

borismul avatar borismul commented on August 17, 2024

There is still a bug here.
Try:
std_notation(9.999999999999999e-05, 3).
'0.00010' #Wrong
std_notation(9.99999999999999e-05, 3).
'0.000100' #Right

from to-precision.

borismul avatar borismul commented on August 17, 2024

I changed this code block:

def _number_profile(value, precision):
  '''
  returns:
    string of significant digits
    10s exponent to get the dot to the proper location in the significant digits
    bool that's true if value is less than zero else false

    created by William Rusnack
      github.com/BebeSparkelSparkel
      linkedin.com/in/williamrusnack/
      [email protected]
    contributions by Thomas Hladish
      github.com/tjhladish
      Issue: github.com/BebeSparkelSparkel/to-precision/issues/5
  '''
  value = float(value)
  is_neg = value < 0
  value = abs(value)

  if value == 0:
    sig_digits = '0' * precision
    power = -(1 - precision)

  else:
    power = -1 * floor(log10(value)) + precision - 1
    
    # issue soved by Thomas Haladish
    # github.com/BebeSparkelSparkel/to-precision/issues/5
    value_power = value * 10.0**power
    if value < 1 and floor(log10(int(round(value_power)))) > floor(log10(int(value_power))):
      power -= 1
    sig_digits = str(int(round(value * 10.0**power))) # cannot use value_power because power is changed

  return sig_digits, int(-power), is_neg

into

def _number_profile(value, precision):
  '''
  returns:
    string of significant digits
    10s exponent to get the dot to the proper location in the significant digits
    bool that's true if value is less than zero else false

    created by William Rusnack
      github.com/BebeSparkelSparkel
      linkedin.com/in/williamrusnack/
      [email protected]
    contributions by Thomas Hladish
      github.com/tjhladish
      Issue: github.com/BebeSparkelSparkel/to-precision/issues/5
  '''
  value = float(value)
  is_neg = value < 0
  value = abs(value)

  if value == 0:
    sig_digits = '0' * precision
    power = -(1 - precision)

  else:
    power = -1 * floor(log10(value)) + precision - 1
    
    # issue soved by Thomas Haladish
    # github.com/BebeSparkelSparkel/to-precision/issues/5
    value_power = value * 10.0**power
    if value < 1 and floor(log10(int(round(value_power))) - sys.float_info.epsilon) > floor(log10(int(value_power))):
      power -= 1
    sig_digits = str(int(round(value * 10.0**power))) # cannot use value_power because power is changed

  return sig_digits, int(-power), is_neg

Note I subtracted one float machine epsilon in the if statement. Import sys as well in the file

from to-precision.

BebeSparkelSparkel avatar BebeSparkelSparkel commented on August 17, 2024

@borismul Thanks for the report. Can you please move this to a PR so that we can examine it and pull it in when we are satisfied?

from to-precision.

 avatar commented on August 17, 2024

Why have you added value < 1 as a condition? The same issue applies for bigger numbers also. For example, _number_profile(999, 2) still returns ("100", 2) instead of ("10", 2). Removing the condition value < 1 seems to fix the issue though

from to-precision.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.