display floats without -e?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

display floats without -e?

Vans S
1> 0.0001.
0.0001
2> 0.00001.
1.0e-5

Once floats get to the 4th/5th decimal place they start being displayed with the e-x notation.  Is there a way to make them always display "normally", for example 0.00001 in this case.  The problem is when you turn the float into a string, its also displayed as 1.0e-5, adding a format function works yes, but gets annoying to maintain when you need to do arithmetic on the floats.  LIke sorting by the value.

Also debugging becomes annoying when you see the e-5, e-6, etc notation everywhere and have to match it up.

Is there a way to compile / add arg to the VM to not do this?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

Dmytro Lytovchenko
A quick answer without much thinking
You can avoid this by specifying precision, i.e.
io_lib:format("~.6f",[F]).

On Mon, 31 Dec 2018 at 17:37, Vans S <[hidden email]> wrote:
1> 0.0001.
0.0001
2> 0.00001.
1.0e-5

Once floats get to the 4th/5th decimal place they start being displayed with the e-x notation.  Is there a way to make them always display "normally", for example 0.00001 in this case.  The problem is when you turn the float into a string, its also displayed as 1.0e-5, adding a format function works yes, but gets annoying to maintain when you need to do arithmetic on the floats.  LIke sorting by the value.

Also debugging becomes annoying when you see the e-5, e-6, etc notation everywhere and have to match it up.

Is there a way to compile / add arg to the VM to not do this?
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

Dan Sommers
In reply to this post by Vans S
On 12/31/18 10:37 AM, Vans S wrote:

 > > 1> 0.0001.
 > 0.0001
 > > 2> 0.00001.
 > 1.0e-5

 > Once floats get to the 4th/5th decimal place they start being
 > displayed with the e-x notation.  Is there a way to make them always
 > display "normally", for example 0.00001 in this case ...

At some point, it's shorter and easier to read with the e-x notation.

Consider a number like 1e-44.  The last thing I want is to have to count
all those zeros.  ;-)

But you probably knew that.

 > ... The problem is when you turn the float into a string, its also
 > displayed as 1.0e-5, adding a format function works yes, but gets
 > annoying to maintain when you need to do arithmetic on the floats.
 > LIke sorting by the value.

I'm confused:  if you're doing arithmetic on the floats, why are you
concerned with the strings?

If you're sorting the strings, then you'll probably either (a) have to
ensure a consistent format, perhaps with something like a ~24.18.0e
specifier, or (b) convert the strings back to floats and suffer various
rounding errors.

 > Also debugging becomes annoying when you see the e-5, e-6, etc
 > notation everywhere and have to match it up.

On that we agree.  :-)

 > Is there a way to compile / add arg to the VM to not do this?

I'm pretty new at Erlang, so I'm probably missing a lot, but how are you
displaying the values now if not with a format function?  (I guess
there's io:write, but at that point, you're awfully close to calling
io:fwrite anyway.)

Are you just using the shell to do immediate calculations?  I'm sure
that the shell has its own default formatting, or maybe it just calls
io:write.

Dan
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

PAILLEAU Eric
Hi,


1> c(floaty).
{ok,floaty}
2> floaty:auto(12.345555).
"12.345555"
3> floaty:auto(12.3455553333333).
"12.3455553333333"
4> list_to_float(floaty:auto(12.3455553333333)).
12.3455553333333




Le 31/12/2018 à 20:31, Dan Sommers a écrit :

> On 12/31/18 10:37 AM, Vans S wrote:
>
>  > > 1> 0.0001.
>  > 0.0001
>  > > 2> 0.00001.
>  > 1.0e-5
>
>  > Once floats get to the 4th/5th decimal place they start being
>  > displayed with the e-x notation.  Is there a way to make them always
>  > display "normally", for example 0.00001 in this case ...
>
> At some point, it's shorter and easier to read with the e-x notation.
>
> Consider a number like 1e-44.  The last thing I want is to have to count
> all those zeros.  ;-)
>
> But you probably knew that.
>
>  > ... The problem is when you turn the float into a string, its also
>  > displayed as 1.0e-5, adding a format function works yes, but gets
>  > annoying to maintain when you need to do arithmetic on the floats.
>  > LIke sorting by the value.
>
> I'm confused:  if you're doing arithmetic on the floats, why are you
> concerned with the strings?
>
> If you're sorting the strings, then you'll probably either (a) have to
> ensure a consistent format, perhaps with something like a ~24.18.0e
> specifier, or (b) convert the strings back to floats and suffer various
> rounding errors.
>
>  > Also debugging becomes annoying when you see the e-5, e-6, etc
>  > notation everywhere and have to match it up.
>
> On that we agree.  :-)
>
>  > Is there a way to compile / add arg to the VM to not do this?
>
> I'm pretty new at Erlang, so I'm probably missing a lot, but how are you
> displaying the values now if not with a format function?  (I guess
> there's io:write, but at that point, you're awfully close to calling
> io:fwrite anyway.)
>
> Are you just using the shell to do immediate calculations?  I'm sure
> that the shell has its own default formatting, or maybe it just calls
> io:write.
>
> Dan
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

floaty.erl (985 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

PAILLEAU Eric
Hi again,
you may add also 'compact' option for unuseful zeros.

4> floaty:auto(3.4e-89).
"0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034"

Warning : documentation says for float_to_list/2 option 'decimals': " If
the number does not fit in the internal static buffer of 256 bytes, the
function throws badarg."


Le 01/01/2019 à 11:43, PAILLEAU Eric a écrit :

> Hi,
>
>
> 1> c(floaty).
> {ok,floaty}
> 2> floaty:auto(12.345555).
> "12.345555"
> 3> floaty:auto(12.3455553333333).
> "12.3455553333333"
> 4> list_to_float(floaty:auto(12.3455553333333)).
> 12.3455553333333
>
>
>
>
> Le 31/12/2018 à 20:31, Dan Sommers a écrit :
>> On 12/31/18 10:37 AM, Vans S wrote:
>>
>>  > > 1> 0.0001.
>>  > 0.0001
>>  > > 2> 0.00001.
>>  > 1.0e-5
>>
>>  > Once floats get to the 4th/5th decimal place they start being
>>  > displayed with the e-x notation.  Is there a way to make them always
>>  > display "normally", for example 0.00001 in this case ...
>>
>> At some point, it's shorter and easier to read with the e-x notation.
>>
>> Consider a number like 1e-44.  The last thing I want is to have to count
>> all those zeros.  ;-)
>>
>> But you probably knew that.
>>
>>  > ... The problem is when you turn the float into a string, its also
>>  > displayed as 1.0e-5, adding a format function works yes, but gets
>>  > annoying to maintain when you need to do arithmetic on the floats.
>>  > LIke sorting by the value.
>>
>> I'm confused:  if you're doing arithmetic on the floats, why are you
>> concerned with the strings?
>>
>> If you're sorting the strings, then you'll probably either (a) have to
>> ensure a consistent format, perhaps with something like a ~24.18.0e
>> specifier, or (b) convert the strings back to floats and suffer various
>> rounding errors.
>>
>>  > Also debugging becomes annoying when you see the e-5, e-6, etc
>>  > notation everywhere and have to match it up.
>>
>> On that we agree.  :-)
>>
>>  > Is there a way to compile / add arg to the VM to not do this?
>>
>> I'm pretty new at Erlang, so I'm probably missing a lot, but how are you
>> displaying the values now if not with a format function?  (I guess
>> there's io:write, but at that point, you're awfully close to calling
>> io:fwrite anyway.)
>>
>> Are you just using the shell to do immediate calculations?  I'm sure
>> that the shell has its own default formatting, or maybe it just calls
>> io:write.
>>
>> Dan
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

Richard O'Keefe
In reply to this post by PAILLEAU Eric
You might want to do some more testing.

11> floaty:auto(12.3455555533).
"12.345555553300000"

It is not clear that this is something you would be happy with.
I discovered this by simplifying the code to

auto(F) when is_float(F) ->
   P = precision(abs(F), 0),
   erlang:float_to_list(F, [{decimals,P}]).

-spec precision(float(), integer()) -> integer().
precision(A, P) ->
    if A == trunc(A) -> P
     ; true          -> precision(A*10.0, P+1)
    end.

and doing some testing of my own.  I was rather relieved to discover
that the original code behaved the same way.

It's really not clear what the aim is here.
I suspect that it is something that should be
done with numbers rather than lists.


On Tue, 1 Jan 2019 at 23:43, PAILLEAU Eric <[hidden email]> wrote:
Hi,


1> c(floaty).
{ok,floaty}
2> floaty:auto(12.345555).
"12.345555"
3> floaty:auto(12.3455553333333).
"12.3455553333333"
4> list_to_float(floaty:auto(12.3455553333333)).
12.3455553333333




Le 31/12/2018 à 20:31, Dan Sommers a écrit :
> On 12/31/18 10:37 AM, Vans S wrote:
>
>  > > 1> 0.0001.
>  > 0.0001
>  > > 2> 0.00001.
>  > 1.0e-5
>
>  > Once floats get to the 4th/5th decimal place they start being
>  > displayed with the e-x notation.  Is there a way to make them always
>  > display "normally", for example 0.00001 in this case ...
>
> At some point, it's shorter and easier to read with the e-x notation.
>
> Consider a number like 1e-44.  The last thing I want is to have to count
> all those zeros.  ;-)
>
> But you probably knew that.
>
>  > ... The problem is when you turn the float into a string, its also
>  > displayed as 1.0e-5, adding a format function works yes, but gets
>  > annoying to maintain when you need to do arithmetic on the floats.
>  > LIke sorting by the value.
>
> I'm confused:  if you're doing arithmetic on the floats, why are you
> concerned with the strings?
>
> If you're sorting the strings, then you'll probably either (a) have to
> ensure a consistent format, perhaps with something like a ~24.18.0e
> specifier, or (b) convert the strings back to floats and suffer various
> rounding errors.
>
>  > Also debugging becomes annoying when you see the e-5, e-6, etc
>  > notation everywhere and have to match it up.
>
> On that we agree.  :-)
>
>  > Is there a way to compile / add arg to the VM to not do this?
>
> I'm pretty new at Erlang, so I'm probably missing a lot, but how are you
> displaying the values now if not with a format function?  (I guess
> there's io:write, but at that point, you're awfully close to calling
> io:fwrite anyway.)
>
> Are you just using the shell to do immediate calculations?  I'm sure
> that the shell has its own default formatting, or maybe it just calls
> io:write.
>
> Dan
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: display floats without -e?

PAILLEAU Eric
Hi Richard,
yes by using 'compact' option of float_to_list/2 you get
1> floaty:auto(12.3455555533).
"12.3455555533"

cheers

Le 01/01/2019 à 12:11, Richard O'Keefe a écrit :

> You might want to do some more testing.
>
> 11> floaty:auto(12.3455555533).
> "12.345555553300000"
>
> It is not clear that this is something you would be happy with.
> I discovered this by simplifying the code to
>
> auto(F) when is_float(F) ->
>     P = precision(abs(F), 0),
>     erlang:float_to_list(F, [{decimals,P}]).
>
> -spec precision(float(), integer()) -> integer().
> precision(A, P) ->
>      if A == trunc(A) -> P
>       ; true          -> precision(A*10.0, P+1)
>      end.
>
> and doing some testing of my own.  I was rather relieved to discover
> that the original code behaved the same way.
>
> It's really not clear what the aim is here.
> I suspect that it is something that should be
> done with numbers rather than lists.
>
>
> On Tue, 1 Jan 2019 at 23:43, PAILLEAU Eric <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>
>     1> c(floaty).
>     {ok,floaty}
>     2> floaty:auto(12.345555).
>     "12.345555"
>     3> floaty:auto(12.3455553333333).
>     "12.3455553333333"
>     4> list_to_float(floaty:auto(12.3455553333333)).
>     12.3455553333333
>
>
>
>
>     Le 31/12/2018 à 20:31, Dan Sommers a écrit :
>      > On 12/31/18 10:37 AM, Vans S wrote:
>      >
>      >  > > 1> 0.0001.
>      >  > 0.0001
>      >  > > 2> 0.00001.
>      >  > 1.0e-5
>      >
>      >  > Once floats get to the 4th/5th decimal place they start being
>      >  > displayed with the e-x notation.  Is there a way to make them
>     always
>      >  > display "normally", for example 0.00001 in this case ...
>      >
>      > At some point, it's shorter and easier to read with the e-x notation.
>      >
>      > Consider a number like 1e-44.  The last thing I want is to have
>     to count
>      > all those zeros.  ;-)
>      >
>      > But you probably knew that.
>      >
>      >  > ... The problem is when you turn the float into a string, its also
>      >  > displayed as 1.0e-5, adding a format function works yes, but gets
>      >  > annoying to maintain when you need to do arithmetic on the floats.
>      >  > LIke sorting by the value.
>      >
>      > I'm confused:  if you're doing arithmetic on the floats, why are you
>      > concerned with the strings?
>      >
>      > If you're sorting the strings, then you'll probably either (a)
>     have to
>      > ensure a consistent format, perhaps with something like a ~24.18.0e
>      > specifier, or (b) convert the strings back to floats and suffer
>     various
>      > rounding errors.
>      >
>      >  > Also debugging becomes annoying when you see the e-5, e-6, etc
>      >  > notation everywhere and have to match it up.
>      >
>      > On that we agree.  :-)
>      >
>      >  > Is there a way to compile / add arg to the VM to not do this?
>      >
>      > I'm pretty new at Erlang, so I'm probably missing a lot, but how
>     are you
>      > displaying the values now if not with a format function?  (I guess
>      > there's io:write, but at that point, you're awfully close to calling
>      > io:fwrite anyway.)
>      >
>      > Are you just using the shell to do immediate calculations?  I'm sure
>      > that the shell has its own default formatting, or maybe it just calls
>      > io:write.
>      >
>      > Dan
>      > _______________________________________________
>      > erlang-questions mailing list
>      > [hidden email] <mailto:[hidden email]>
>      > http://erlang.org/mailman/listinfo/erlang-questions
>      >
>
>     _______________________________________________
>     erlang-questions mailing list
>     [hidden email] <mailto:[hidden email]>
>     http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions