Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can you help guys?

58 views
Skip to first unread message

brokolists

unread,
Apr 23, 2015, 11:51:32 AM4/23/15
to
my problem is i m working with very long float numbers and i use
numberx =float(( input( 'enter the number\n ')))
after i use this command when i enter something more than 10 or 11 digits it uses like 1e+10 or something like that
but i have to calculate it without scientific 'e' type. what can i do to solve this?
(sorry for my bad english)

Ian Kelly

unread,
Apr 23, 2015, 2:45:20 PM4/23/15
to Python
That's just how the number is formatted for output by default. It has
nothing to do with how the number is handled in memory. You can
prevent it from outputting scientific notation using the 'f' format
specifier. Pick whichever example best suits your use.

>>> num = 12345678901234567890.
>>> print(num)
1.2345678901234567e+19
>>> print('%f' % num) # Using percent formatting
12345678901234567168.000000
>>> print('{:f}'.format(num)) # Using the str.format method
12345678901234567168.000000
>>> print(format(num, 'f')) # Using the format builtin
12345678901234567168.000000

Now, if you're working with very long floats then you're probably also
running into precision issues, as you can see in the examples above.
If the precision is important to you, then you may want to consider
using decimals instead of floats.

Steven D'Aprano

unread,
Apr 23, 2015, 7:20:12 PM4/23/15
to
Are you sure? Can you give an example? This works for me:

py> s = input("Enter a number: ")
Enter a number: 0.123456789012345
py> len(s) - 2 # Number of digits
15
py> x = float(s)
py> print(x)
0.123456789012345

Maybe you shouldn't be using floats:

py> n = int(input("Enter a number: "))
Enter a number: 123456789012345678901234567890
py> print(n)
123456789012345678901234567890
py> print(n+1)
123456789012345678901234567891


Integers will not use scientific "e" notation, but floats will. But that is
only the *display* format:

py> x = float(input("Enter a number in Sci format: "))
Enter a number in Sci format: 1.234e10
py> print(x)
12340000000.0
py> print("%f" % x)
12340000000.000000
py> print("%g" % x)
1.234e+10

Notice that displaying the float with %f and %g format codes look different,
but are the same number.

You will need to explain more about what you are trying to do before we can
give you good advise. Perhaps if you show us some examples?




--
Steven

Message has been deleted

brokolists

unread,
Apr 24, 2015, 4:29:52 AM4/24/15
to
24 Nisan 2015 Cuma 02:20:12 UTC+3 tarihinde Steven D'Aprano yazdı:
i fixed the "e" problem by using digits

getcontext().prec=20
it works fine but in my algoritm it was checking the answers by their lenght so this created a new problem by giving all the answers with 20 digits. I might fix this but i have searched for 4 hours just how to get rid of 'e' . i think there must be a easier way to do this.



Dave Angel

unread,
Apr 24, 2015, 7:07:58 AM4/24/15
to pytho...@python.org
On 04/24/2015 04:29 AM, brokolists wrote:
> 24 Nisan 2015 Cuma 02:20:12 UTC+3 tarihinde Steven D'Aprano yazdı:
>> On Fri, 24 Apr 2015 01:51 am, brokolists wrote:
>>
>>> my problem is i m working with very long float numbers and i use
>>> numberx =float(( input( 'enter the number\n ')))
>>> after i use this command when i enter something more than 10 or 11 digits
>>> it uses like 1e+10 or something like that but i have to calculate it
>>> without scientific 'e' type. what can i do to solve this? (sorry for my
>>> bad english)
>>
>>

>
> i fixed the "e" problem by using digits
>
> getcontext().prec=20
> it works fine but in my algoritm it was checking the answers by their lenght so this created a new problem by giving all the answers with 20 digits. I might fix this but i have searched for 4 hours just how to get rid of 'e' . i think there must be a easier way to do this.
>

We could be a lot more helpful if you just spelled it out. digits and
getcontext() are not builtins, so we don't really know how you're using
them, except by guessing.

You've changed the code, probably by using one of the many suggestions
here. But what's it look like now?

You should have started the thread by telling us the Python version.
I'm guessing you're using python 3.x


In Python 3.x, input() returns a string. So you can tell how much the
user typed by doing a len() on that string. Once you convert it into
some other form, you're choosing the number of digits partly by how you
convert it.

Can you state what your real assignment is? And what code you have so
far? And just what is wrong with it, that you need to change?

For example, if the "correct" answer for your user is "7.3" and the
user types "7.300" is that supposed to be right, or wrong?




--
DaveA

brokolists

unread,
Apr 24, 2015, 11:31:48 AM4/24/15
to
i fixed the problem . ty for helps btw.
the problem was after i started using Decimal was i was cheking the outputs based on their lenght and when you type
getcontext().prec=20 ;
it returns allmost every mathematical output based on 20 digits eventhough
when they are not neccessary like 20.00000...00 = 20
the code was to fix this was there all the time and i mannaged to find it eventually ;
Decimal.normalize(x); it deletes the unneccesary zeros.
ty for helps btw.
0 new messages