def format_float_scientific(expr, digits_before, decimals, force_dot): """ Put a float in scientific format. """ work_digits = digits_before + decimals if work_digits > expr.digits: # decimal precision of the type work_digits = expr.digits if expr.is_zero(): if not force_dot: if expr.exp_sign == 'E': return 'E+00' return '0D+00' # matches GW output. odd, odd, odd digitstr, exp10 = '0'*(digits_before+decimals), 0 else: if work_digits > 0: # scientific representation lim_bot = just_under(pow_int(expr.ten, work_digits-1)) else: # special case when work_digits == 0, see also below # setting to 0.1 results in incorrect rounding (why?) lim_bot = expr.one.copy() lim_top = lim_bot.copy().imul10() num, exp10 = expr.bring_to_range(lim_bot, lim_top) digitstr = get_digits(num, work_digits) if len(digitstr) < digits_before + decimals: digitstr += '0' * (digits_before + decimals - len(digitstr)) # this is just to reproduce GW results for no digits: # e.g. PRINT USING "#^^^^";1 gives " E+01" not " E+00" if work_digits == 0: exp10 += 1 exp10 += digits_before + decimals - 1 return scientific_notation(digitstr, exp10, expr.exp_sign, digits_to_dot=digits_before, force_dot=force_dot)
def format_float_scientific(expr, digits_before, decimals, force_dot): """ Put a float in scientific format. """ work_digits = digits_before + decimals if work_digits > expr.digits: # decimal precision of the type work_digits = expr.digits if expr.is_zero(): if not force_dot: if expr.exp_sign == 'E': return 'E+00' return '0D+00' # matches GW output. odd, odd, odd digitstr, exp10 = '0' * (digits_before + decimals), 0 else: if work_digits > 0: # scientific representation lim_bot = just_under(pow_int(expr.ten, work_digits - 1)) else: # special case when work_digits == 0, see also below # setting to 0.1 results in incorrect rounding (why?) lim_bot = expr.one.copy() lim_top = lim_bot.copy().imul10() num, exp10 = expr.bring_to_range(lim_bot, lim_top) digitstr = get_digits(num, work_digits) if len(digitstr) < digits_before + decimals: digitstr += '0' * (digits_before + decimals - len(digitstr)) # this is just to reproduce GW results for no digits: # e.g. PRINT USING "#^^^^";1 gives " E+01" not " E+00" if work_digits == 0: exp10 += 1 exp10 += digits_before + decimals - 1 return scientific_notation(digitstr, exp10, expr.exp_sign, digits_to_dot=digits_before, force_dot=force_dot)
def format_float_fixed(expr, decimals, force_dot): """ Put a float in fixed-point representation. """ unrounded = mul(expr, pow_int(expr.ten, decimals)) # expr * 10**decimals num = unrounded.copy().iround() # find exponent exp10 = 1 pow10 = pow_int(expr.ten, exp10) # pow10 = 10L**exp10 while num.gt(pow10) or num.equals(pow10): # while pow10 <= num: pow10.imul10() # pow10 *= 10 exp10 += 1 work_digits = exp10 + 1 diff = 0 if exp10 > expr.digits: diff = exp10 - expr.digits num = div(unrounded, pow_int(expr.ten, diff)).iround() # unrounded / 10**diff work_digits -= diff num = num.trunc_to_int() # argument work_digits-1 means we're getting work_digits==exp10+1-diff digits # fill up with zeros digitstr = get_digits(num, work_digits-1, remove_trailing=False) + ('0' * diff) return decimal_notation(digitstr, work_digits-1-1-decimals+diff, '', force_dot)
def format_float_fixed(expr, decimals, force_dot): """ Put a float in fixed-point representation. """ unrounded = mul(expr, pow_int(expr.ten, decimals)) # expr * 10**decimals num = unrounded.copy().iround() # find exponent exp10 = 1 pow10 = pow_int(expr.ten, exp10) # pow10 = 10L**exp10 while num.gt(pow10) or num.equals(pow10): # while pow10 <= num: pow10.imul10() # pow10 *= 10 exp10 += 1 work_digits = exp10 + 1 diff = 0 if exp10 > expr.digits: diff = exp10 - expr.digits num = div(unrounded, pow_int(expr.ten, diff)).iround() # unrounded / 10**diff work_digits -= diff num = num.trunc_to_int() # argument work_digits-1 means we're getting work_digits==exp10+1-diff digits # fill up with zeros digitstr = get_digits(num, work_digits - 1, remove_trailing=False) + ('0' * diff) return decimal_notation(digitstr, work_digits - 1 - 1 - decimals + diff, '', force_dot)