[Glass] Is there a way to print a Float with all decimals but without exponential notation?

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

[Glass] Is there a way to print a Float with all decimals but without exponential notation?

Mariano Martinez Peck
Hi guys.

I am exporting some data to a CSV file and I am finding some differences with Pharo. For my use-case, I would like to:

- Print all decimals of the float
- do not round
- do not show exponential notation

#greaseString (or doing self printOn: strm base: 10.) rounds the string. For example:

19896.800000000003 greaseString 
'19896.8'

while in Pharo I get

--
Mariano
http://marianopeck.wordpress.com

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Mariano Martinez Peck
Hi guys.

I am exporting some data to a CSV file and I am finding some differences with Pharo. For my use-case, I would like to:

- Print all decimals of the float
- do not round
- do not show exponential notation

#greaseString (or doing self printOn: strm base: 10.) rounds the string. For example:

19896.800000000003 greaseString 
-> '19896.8'

while in Pharo I get (what I want)

19896.800000000003 greaseString 
-> '19896.800000000003'


#asStringUsingFormat: allows me to NOT use exponential notion..but I am forced to define the first and second parameter as well :(

#asString shows me all decimals but it uses the exponential notion:

19896.800000000003 asString 
-> '1.9896800000000003E+04'


So what I need is a kind of #asString but not using exponential notion. Is that possible? 


Thanks in advance, 

On Mon, Mar 3, 2014 at 10:42 AM, Mariano Martinez Peck <[hidden email]> wrote:
Hi guys.

I am exporting some data to a CSV file and I am finding some differences with Pharo. For my use-case, I would like to:

- Print all decimals of the float
- do not round
- do not show exponential notation

#greaseString (or doing self printOn: strm base: 10.) rounds the string. For example:

19896.800000000003 greaseString 
'19896.8'

while in Pharo I get

--
Mariano
http://marianopeck.wordpress.com



--
Mariano
http://marianopeck.wordpress.com

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Martin McClure-5
On 03/03/2014 05:46 AM, Mariano Martinez Peck wrote:

> Hi guys.
>
> I am exporting some data to a CSV file and I am finding some differences
> with Pharo. For my use-case, I would like to:
>
> - Print all decimals of the float
> - do not round
> - do not show exponential notation
>
> #greaseString (or doing self printOn: strm base: 10.) rounds the string.
> For example:
>
> 19896.800000000003 greaseString
> -> '19896.8'
>
> while in Pharo I get (what I want)
>
> 19896.800000000003 greaseString
> -> '19896.800000000003'
>
>
> #asStringUsingFormat: allows me to NOT use exponential notion..but I am
> forced to define the first and second parameter as well :(
>
> #asString shows me all decimals but it uses the exponential notion:
>
> 19896.800000000003 asString
> -> '1.9896800000000003E+04'
>
>
> So what I need is a kind of #asString but not using exponential notion.
> Is that possible?

In order to unambiguously identify an arbitrary 64-bit Float, you need
17 significant decimal digits. So it's a bit clunky, but this should
work for most cases:

| num |
num := 19896.800000000003.
^num asStringUsingFormat: {0. 17 - (num truncated printString size). false}

--> '19896.800000000003'

This assumes that your number is >= 1.0. If your number is ever
negative, or if it's between 0 and 1, the calculation of the number of
digits to the right of the decimal place would need a bit more
complexity, since the leading character would be $- or $0, neither of
which count as a "significant decimal digit".

Regards,

-Martin
_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Mariano Martinez Peck



On Mon, Mar 3, 2014 at 3:32 PM, Martin McClure <[hidden email]> wrote:
On 03/03/2014 05:46 AM, Mariano Martinez Peck wrote:
> Hi guys.
>
> I am exporting some data to a CSV file and I am finding some differences
> with Pharo. For my use-case, I would like to:
>
> - Print all decimals of the float
> - do not round
> - do not show exponential notation
>
> #greaseString (or doing self printOn: strm base: 10.) rounds the string.
> For example:
>
> 19896.800000000003 greaseString
> -> '19896.8'
>
> while in Pharo I get (what I want)
>
> 19896.800000000003 greaseString
> -> '19896.800000000003'
>
>
> #asStringUsingFormat: allows me to NOT use exponential notion..but I am
> forced to define the first and second parameter as well :(
>
> #asString shows me all decimals but it uses the exponential notion:
>
> 19896.800000000003 asString
> -> '1.9896800000000003E+04'
>
>
> So what I need is a kind of #asString but not using exponential notion.
> Is that possible?

In order to unambiguously identify an arbitrary 64-bit Float, you need
17 significant decimal digits. So it's a bit clunky, but this should
work for most cases:

| num |
num := 19896.800000000003.
^num asStringUsingFormat: {0. 17 - (num truncated printString size). false}

--> '19896.800000000003'

This assumes that your number is >= 1.0. If your number is ever
negative, or if it's between 0 and 1, the calculation of the number of
digits to the right of the decimal place would need a bit more
complexity, since the leading character would be $- or $0, neither of
which count as a "significant decimal digit".

Hi Martin,

Yes, that looks a bit clunky and probably slow? I am writing reports where I print lots of floats, so this may be important.
Also, yes, my numbers can be negative and even between 0 and 1.0. Also, not only Float but SmallDouble as well....

Wish there could be a simple method (primitive) to print all type of 64-bits floats without the exponential notion...

For the moment I guess I will stay with the exponential :(

Thanks anyway,


--
Mariano
http://marianopeck.wordpress.com

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Martin McClure-5
>
> Hi Martin,
>
> Yes, that looks a bit clunky and probably slow? I am writing reports
> where I print lots of floats, so this may be important.
> Also, yes, my numbers can be negative and even between 0 and 1.0. Also,
> not only Float but SmallDouble as well....
>
> Wish there could be a simple method (primitive) to print all type of
> 64-bits floats without the exponential notion...
>
> For the moment I guess I will stay with the exponential :(

Hi Mariano,

Clunky it definitely is. Though the *real* algorithm would be a lot more
complicated.

But how slow is slow? How many is "lots" and how often do you need to
print them? When it comes to performance, assumptions are very risky. I
recommend trying it and testing what the performance is, rather than
assuming it will be too slow. You can probably do that kind of test in
half an hour or so.

SmallDouble and Float should be completely polymorphic (at least plenty
polymorphic for this job), so you shouldn't have to worry about the
difference. The handling of negative and 0 - 1 won't add much complexity.

Regards,

-Martin
_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Mariano Martinez Peck



On Tue, Mar 4, 2014 at 4:39 PM, Martin McClure <[hidden email]> wrote:

Hi Martin,

Yes, that looks a bit clunky and probably slow? I am writing reports
where I print lots of floats, so this may be important.
Also, yes, my numbers can be negative and even between 0 and 1.0. Also,
not only Float but SmallDouble as well....

Wish there could be a simple method (primitive) to print all type of
64-bits floats without the exponential notion...

For the moment I guess I will stay with the exponential :(

Hi Mariano,

Clunky it definitely is. Though the *real* algorithm would be a lot more complicated.

But how slow is slow? How many is "lots" and how often do you need to print them? When it comes to performance, assumptions are very risky. I recommend trying it and testing what the performance is, rather than assuming it will be too slow. You can probably do that kind of test in half an hour or so.


Yes, probably I am exaggerating ;)
 
SmallDouble and Float should be completely polymorphic (at least plenty polymorphic for this job), so you shouldn't have to worry about the difference. The handling of negative and 0 - 1 won't add much complexity.

OK...so let's try implementing that method. Is it as simple as this or I am missing something?

Float >> asStringWithAllDecimalsAndNoExponentialNotation
| truncatedStringSize |
truncatedStringSize := self truncated printString size.
(self >= 1.0) 
ifTrue: [ ^ self asStringUsingFormat: {0. 17 - truncatedStringSize. false} ]
ifFalse: [ ^ self asStringUsingFormat: {0. 18 - truncatedStringSize. false} ]

 
Thanks!



--
Mariano
http://marianopeck.wordpress.com

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] Is there a way to print a Float with all decimals but without exponential notation?

Martin McClure-5
On 03/04/2014 12:12 PM, Mariano Martinez Peck wrote:

>
>     But how slow is slow? How many is "lots" and how often do you need
>     to print them? When it comes to performance, assumptions are very
>     risky. I recommend trying it and testing what the performance is,
>     rather than assuming it will be too slow. You can probably do that
>     kind of test in half an hour or so.
>
>
> Yes, probably I am exaggerating ;)
>
>     SmallDouble and Float should be completely polymorphic (at least
>     plenty polymorphic for this job), so you shouldn't have to worry
>     about the difference. The handling of negative and 0 - 1 won't add
>     much complexity.
>
>
> OK...so let's try implementing that method. Is it as simple as this or I
> am missing something?
>
> Float >> asStringWithAllDecimalsAndNoExponentialNotation
> | truncatedStringSize |
> truncatedStringSize := self truncated printString size.
> (self >= 1.0)
> ifTrue: [ ^ self asStringUsingFormat: {0. 17 - truncatedStringSize. false} ]
> ifFalse: [ ^ self asStringUsingFormat: {0. 18 - truncatedStringSize.
> false} ]

Yes, I think it is that simple. Though I'd write a few test cases using
numbers between 0 and 1, -0 and -1, and of larger absolute values, just
to be sure.

Regards,

-Martin
_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass