Fun with Number readFrom: What should we do ?

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

Re: Fun with Number readFrom: What should we do ?

Andreas.Raab
Francisco Garau wrote:
 > This would be both flexible and generic.

And complete and utter overkill in my understanding ;-) Yes, of course,
you can do what you just described but by the end of the day, there are
two prevalent use cases: 1) I want to read a "Squeak number", e.g., I
want to read whatever is considered a valid numeric literal in Squeak.
2) I want to read "something else" and it doesn't matter whether that
"something else" is a roman number, a scientific one, a .csv data
record. What does matter is that Squeak cannot possibly provide parsers
for all numeric formats ever in existence, and neither should it. What
it should do is to read its own literals correctly and quickly.

Therefore (coming back to the starting point of this discussion) for
Number/Integer/Float>>readFrom: (which by definition are within use case
1) we should not overly generalize reading numbers to the point where
it's both slow and gets nothing quite right (since there are almost
certainly ambiguous representations). What we should do is to fix what
is broken and nothing more; namely that reading a number from an empty
string answers zero.

And if you feel that you need more flexibility and format support than
Squeak currently provides, feel free to set up a Numeric Parser project
on SqueakSource and put that parser there fore anyone to use who needs
it. This leaves plenty of room for experimentation (like substituting
Number>>readFrom: in the way you are suggesting) and doesn't get into
the way of fixing the bug that started the whole discussion.

Cheers,
   - Andreas

Francisco Garau wrote:

> I totally agree with Diego.
>
> Moreover, I think that each message should do the parsing on exactly one
> format. Something like this:
>
>    (SqNumberParser on: '1.23') number
>    (SqNumberParser on: '1.23e4) scientificNotation; number
>    (SqNumberParser on: '2r1111') base2Notation; number
>    (SqNumberParser on: '') cvsNotation; number "would answer 0"
>
> And then we can add a top level method to try several alternatives (in a
> specific order)
>
>    SqNumberParser>>parse
>        try scientificNotation ifTrue: [^got it]
>        try xxxNotation ifTrue: [^got it]
>        try base2Notation ifTrue: [^got it]
>        try base16Notation ifTrue: [^got it]
>        else ParseException signal
>
> This would be both flexible and generic. Those who know precisely the
> string format of an expected number, can use the specific parse message.
> And if you don't know the string format, just use the generic parse
> message.
>
> String>>sqNumber
>    ^(SqNumberParser on: self) parse; number
>
> By using the Sq prefix, we would avoid clashing with existing methods.
> The code that is relying on the current funny behaviour would remain
> unaffected.
>
> Cheers,
> Francisco
>
>
> ----- Original Message ----- From: "Diego Fernandez" <[hidden email]>
> To: <[hidden email]>; "The general-purpose Squeak developers list"
> <[hidden email]>
> Sent: Thursday, April 27, 2006 2:59 PM
> Subject: Re: Fun with Number readFrom: What should we do ?
>
>
>> My proposition is:
>>
>> Number>>readFrom: aStringOrStream
>>     ^self readFrom: aStringOrStream ifFail: [^self error: 'cannot read a
>> number']
>>
>> If nil is what you want, you will have to write:
>>     value := Number readFrom: aStream ifFail: [nil].
>> A little longer than your proposition:
>>     value := Number readfrom: aStream.
>
> The mine is:
> Number>>readFrom: aStringOrStream
>      ^NumberParser new parse: aStringOrStream
>
> NumberParser>>parse: aStringOrStream
>       "on failure"
>        NumberParsingException signal
>        "or just ParseException"
>
> I think that #readFrom: must be an extension of Number.
> So if you want more control of the parsing you can configure an
> instance of NumberParser.
> (may be you can have a FortranNumberParser :)).
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Nicolas Cellier-3
In reply to this post by Nicolas Cellier-3

What about these syntaxes, should we keep them ?
    Number readFrom: '2r-11'.
    Number readFrom: '-2r-11'.

They are not in VW. Are they ANSI, ST-80 or whatever ?
Is Squeak syntax officially defined anywhere (EBNF) ?

Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Milan Zimmermann-2
In reply to this post by Nicolas Cellier-3
On 2006 April 26 17:34, nicolas cellier wrote:

> Hello all,
>
> i noticed that Number readFrom: silently answers 0 when it does not
> encounter some digits:
>
>     Number readFrom: '' readStream.
>     Number readFrom: 'foo-bar' readStream.
>     Number readFrom: '.' readStream.
>
> will all answer 0. Well, i believe there must have been discussions whether
> or not raising an exception in this case, at least recently in vw list. But
> let us say that answering 0 is a feature.

Would it make sense to answer NaN?

Milan

(If "Number" was "float", is there a rule for such conversion in IEEE754?)

>
>
> Then try:
>
>     Number readFrom: 'rules are made for the others' readStream.
>
> fail with an error: 'Invalid radix'.
> Well, well, don't tell me it is a feature.
>
>
> And just for fun, this one:
>
>     Number readFrom: '.e' readStream.
>
> ah, another feature UndefinedObject doesNotUnderstand: #digitValue.
>
>
> Still want to play ? Try this one:
>
>     Number readFrom: '--1' readStream.
>
> Amazing what we can do with just one readFrom:...
>
>
> Funny, it also accept some un-Smalltalk-ish syntax:
>
>     Number readFrom: '.1e2'.
>     Number readFrom: '1.e1'.
>
> will answer a Float (10.0 like FORTRAN).
> I suppose this one is intentionnaly a feature.
>
>
> I will not analyze here the cases when the stream advance, and the cases
> when it does not... That would be boring...
>
>
> Now i am trying to implement Float readFrom without accumulating rounding
> errors, and this can be the right time for deciding about these cases.
>
> What do you vote for ? exception, answering 0, any other idea ?
> What to do with the stream in case of error ? rewind, keep in place ?
> I'am waiting for your comments.
>
> Nicolas

Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Martin Wirblat
In reply to this post by Andreas.Raab
Andreas Raab wrote:

> The example of the csv file is very badly chosen IMO. CSV files contain
> many different things and applying the same logic you are using for
> numbers here would dictate that String>>readFrom: also answer nil when
> trying to read from an empty string. Moreover, it would dictate that
> Strings use double quotes (if I remember my .csv corretly). This has
> nothing to do with the discussion here - .csv files do NOT contain
> "Smalltalk numbers" so to speak, therefore they need to be separately
> parsed and analyzed and arguing that an empty string should parse as nil
> because .csv files have empty fields is really besides the point of this
> discussion.
>

I want to parse 1.23 and that is not a Smalltalk number, it is a number.
If I read '' from a csv file, from a user input or a free-form text as
what could or should be a number, the data point is undefined, and nil
is expressing that literally.

> Number>>readFrom: has the specific intent to read a literal number from
> a stream. I have yet to find a caller that could even deal with the fact
> that this method may answer nil. This goes mostly to show at what a low
> level number parsing is typically used (e.g., all the error checking
> happens mostly at a higher level) which speaks very clearly for having
> an exception raised to signal the problem.
>

There is no problem or error unless you define that Number>>readFrom:
has to be and can only be used when Squeak had written a "Smalltalk
number" out and now has to get it back.

> @Nicolas: I find your proposal of readFrom:ifFail: to be somewhat
> overkill. An exception/error seems simpler and more direct to me.
>

Prohibiting overkill solutions was the pragmatical point of my suggestion.

Regards,
Martin

Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

stéphane ducasse-2
In reply to this post by Francisco Garau-2
submit  tests!

Stef

On 28 avr. 06, at 01:21, Francisco Garau wrote:

> I totally agree with Diego.
>
> Moreover, I think that each message should do the parsing on  
> exactly one format. Something like this:
>
>    (SqNumberParser on: '1.23') number
>    (SqNumberParser on: '1.23e4) scientificNotation; number
>    (SqNumberParser on: '2r1111') base2Notation; number
>    (SqNumberParser on: '') cvsNotation; number "would answer 0"
>
> And then we can add a top level method to try several alternatives  
> (in a specific order)
>
>    SqNumberParser>>parse
>        try scientificNotation ifTrue: [^got it]
>        try xxxNotation ifTrue: [^got it]
>        try base2Notation ifTrue: [^got it]
>        try base16Notation ifTrue: [^got it]
>        else ParseException signal
>
> This would be both flexible and generic. Those who know precisely  
> the string format of an expected number, can use the specific parse  
> message. And if you don't know the string format, just use the  
> generic parse message.
>
> String>>sqNumber
>    ^(SqNumberParser on: self) parse; number
>
> By using the Sq prefix, we would avoid clashing with existing  
> methods. The code that is relying on the current funny behaviour  
> would remain unaffected.
>
> Cheers,
> Francisco
>
>
> ----- Original Message ----- From: "Diego Fernandez"  
> <[hidden email]>
> To: <[hidden email]>; "The general-purpose Squeak developers  
> list" <[hidden email]>
> Sent: Thursday, April 27, 2006 2:59 PM
> Subject: Re: Fun with Number readFrom: What should we do ?
>
>
>> My proposition is:
>>
>> Number>>readFrom: aStringOrStream
>>     ^self readFrom: aStringOrStream ifFail: [^self error: 'cannot  
>> read a
>> number']
>>
>> If nil is what you want, you will have to write:
>>     value := Number readFrom: aStream ifFail: [nil].
>> A little longer than your proposition:
>>     value := Number readfrom: aStream.
>
> The mine is:
> Number>>readFrom: aStringOrStream
>      ^NumberParser new parse: aStringOrStream
>
> NumberParser>>parse: aStringOrStream
>       "on failure"
>        NumberParsingException signal
>        "or just ParseException"
>
> I think that #readFrom: must be an extension of Number.
> So if you want more control of the parsing you can configure an
> instance of NumberParser.
> (may be you can have a FortranNumberParser :)).
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

stéphane ducasse-2
In reply to this post by Andreas.Raab
+ 1

Stef

On 28 avr. 06, at 01:45, Andreas Raab wrote:

> Francisco Garau wrote:
> > This would be both flexible and generic.
>
> And complete and utter overkill in my understanding ;-) Yes, of  
> course, you can do what you just described but by the end of the  
> day, there are two prevalent use cases: 1) I want to read a "Squeak  
> number", e.g., I want to read whatever is considered a valid  
> numeric literal in Squeak. 2) I want to read "something else" and  
> it doesn't matter whether that "something else" is a roman number,  
> a scientific one, a .csv data record. What does matter is that  
> Squeak cannot possibly provide parsers for all numeric formats ever  
> in existence, and neither should it. What it should do is to read  
> its own literals correctly and quickly.
>
> Therefore (coming back to the starting point of this discussion)  
> for Number/Integer/Float>>readFrom: (which by definition are within  
> use case 1) we should not overly generalize reading numbers to the  
> point where it's both slow and gets nothing quite right (since  
> there are almost certainly ambiguous representations). What we  
> should do is to fix what is broken and nothing more; namely that  
> reading a number from an empty string answers zero.
>
> And if you feel that you need more flexibility and format support  
> than Squeak currently provides, feel free to set up a Numeric  
> Parser project on SqueakSource and put that parser there fore  
> anyone to use who needs it. This leaves plenty of room for  
> experimentation (like substituting Number>>readFrom: in the way you  
> are suggesting) and doesn't get into the way of fixing the bug that  
> started the whole discussion.
>
> Cheers,
>   - Andreas
>
> Francisco Garau wrote:
>> I totally agree with Diego.
>> Moreover, I think that each message should do the parsing on  
>> exactly one format. Something like this:
>>    (SqNumberParser on: '1.23') number
>>    (SqNumberParser on: '1.23e4) scientificNotation; number
>>    (SqNumberParser on: '2r1111') base2Notation; number
>>    (SqNumberParser on: '') cvsNotation; number "would answer 0"
>> And then we can add a top level method to try several alternatives  
>> (in a specific order)
>>    SqNumberParser>>parse
>>        try scientificNotation ifTrue: [^got it]
>>        try xxxNotation ifTrue: [^got it]
>>        try base2Notation ifTrue: [^got it]
>>        try base16Notation ifTrue: [^got it]
>>        else ParseException signal
>> This would be both flexible and generic. Those who know precisely  
>> the string format of an expected number, can use the specific  
>> parse message. And if you don't know the string format, just use  
>> the generic parse message.
>> String>>sqNumber
>>    ^(SqNumberParser on: self) parse; number
>> By using the Sq prefix, we would avoid clashing with existing  
>> methods. The code that is relying on the current funny behaviour  
>> would remain unaffected.
>> Cheers,
>> Francisco
>> ----- Original Message ----- From: "Diego Fernandez"  
>> <[hidden email]>
>> To: <[hidden email]>; "The general-purpose Squeak developers  
>> list" <[hidden email]>
>> Sent: Thursday, April 27, 2006 2:59 PM
>> Subject: Re: Fun with Number readFrom: What should we do ?
>>> My proposition is:
>>>
>>> Number>>readFrom: aStringOrStream
>>>     ^self readFrom: aStringOrStream ifFail: [^self error: 'cannot  
>>> read a
>>> number']
>>>
>>> If nil is what you want, you will have to write:
>>>     value := Number readFrom: aStream ifFail: [nil].
>>> A little longer than your proposition:
>>>     value := Number readfrom: aStream.
>> The mine is:
>> Number>>readFrom: aStringOrStream
>>      ^NumberParser new parse: aStringOrStream
>> NumberParser>>parse: aStringOrStream
>>       "on failure"
>>        NumberParsingException signal
>>        "or just ParseException"
>> I think that #readFrom: must be an extension of Number.
>> So if you want more control of the parsing you can configure an
>> instance of NumberParser.
>> (may be you can have a FortranNumberParser :)).
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

francisco.j.garau
In reply to this post by Nicolas Cellier-3
Andreas,

I completely agree with what you said except for the first sentence. Why
do you think it is a "complete and utter overkill"? I can think of three
possibilities:

1) The extra class you are adding to the image (SqNumberParser)
2) The extra object involved in the parsing
3) All the explicit methods that you have to define on the SqNumberParser
object.

None of them seems an overkill to me. I am obviously missing something.

Cheers,
Francisco


This communication is for informational purposes only. It is not intended
as an offer or solicitation for the purchase or sale of any financial
instrument or as an official confirmation of any transaction. All market prices,
data and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Nicolas Cellier-3
Le Vendredi 28 Avril 2006 18:27, [hidden email] a écrit :

> Andreas,
>
> I completely agree with what you said except for the first sentence. Why
> do you think it is a "complete and utter overkill"? I can think of three
> possibilities:
>
> 1) The extra class you are adding to the image (SqNumberParser)
> 2) The extra object involved in the parsing
> 3) All the explicit methods that you have to define on the SqNumberParser
> object.
>
> None of them seems an overkill to me. I am obviously missing something.
>
> Cheers,
> Francisco
>

I added the bug report and a few test cases at
http://bugs.impara.de/view.php?id=3512 last night.

I have a first implementation of SqNumberParser.
It is more convenient to have a class than a set of messages (see how many are
necessary in Number>>readFrom:), because we can store some intermediate
results and better optimize Float reading (number of trailing zeroes is an
example that can save useless computations). Relax, creating a class does not
spoil efficiency.

The second advantage of SqNumberParser is that it is a good starting point for
those wanting their own extension for Number parsing as suggested by Diego
and Francisco.
The class could easily have other extensions like indicating the error in the
TextEditor, like for code...

So far, i suppressed the bugs i know, and the class read Integer ScaledDecimal
and Float (including nan infinity and negative zero), and pass all the
NumberParsingTests successfully.

However i still have the bellerophon algorithm (from tony's reference) failing
with last bit incorrect in a few cases (i can check that with asTrueFraction
for number greater than 1.0 timestwoPower: 53). So i do not release it yet.

Float>>absPrintOnExactly;base: works rather well and is a good candidate for
testing readFrom:. However, i do not over trust it, because it includes the
same code that bugged absPrintOn: (See note in
http://bugs.impara.de/view.php?id=3493) : it adds 1 to a digit without
testing if digit=(base-9)... I did not reveal the bug yet.

Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Nicolas Cellier-3
In reply to this post by Martin Wirblat
>
> I want to parse 1.23 and that is not a Smalltalk number, it is a number.
> If I read '' from a csv file, from a user input or a free-form text as
> what could or should be a number, the data point is undefined, and nil
> is expressing that literally.
>

Hi Martin,
I persist to think that getting a nil is rarely the aimed feature, and it will
mostly be used as a C-style way to indicate a failure (exactly like fopen()
returning NULL).
As Andreas suggested, browse senders of readFrom: in a 3.9 image, and tell us
where a nil answer would make sense.

Doing so, you will notice a single ifError: [].
What we can conclude is that no-one is prepared for the possibility of
failure... So, some code might have to be debugged anyway when we change the
behavior and do not answer 0 anymore. (or maybe we will found so far hidden
bugs).

If we adopt ^nil, methods will not fail while reading, they will fail latter
when using the value, and that may be far latter, making the bug less
explicit to track.
This is true for future code also, we are all either lazy, or not prepared to
write C-style-code, and a few will assert ifNil:.

If we adopt the exception, the method will fail, but with debugger opened
exactly at the place where the origin of the problem is.

And if you really want the nil, no problem, you write
    value := [Number readFrom: stream] ifError: [nil].
With a resumable exception, you can easily catch that once for all
    [ MyCvsApp doSomething ] on: NumberParsingError do: [:exc | exc resume:
nil].

One can also resume: 0, if one's app requires this as a feature...

Sorry that you have to write extra-code, but it seems that our laziness is
superior to yours...

Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Diego Fernández
In reply to this post by Nicolas Cellier-3
On 4/28/06, nicolas cellier <[hidden email]> wrote:
> I added the bug report and a few test cases at
> http://bugs.impara.de/view.php?id=3512 last night.
>
> I have a first implementation of SqNumberParser.

Excellent!!!
Since the last mail, I was trying to do the same (a separate number
parser), but I didn't have time. :)

Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

stéphane ducasse-2
In reply to this post by Nicolas Cellier-3
>
> I added the bug report and a few test cases at
> http://bugs.impara.de/view.php?id=3512 last night.
>
> I have a first implementation of SqNumberParser.
> It is more convenient to have a class than a set of messages (see  
> how many are
> necessary in Number>>readFrom:), because we can store some  
> intermediate
> results and better optimize Float reading (number of trailing  
> zeroes is an
> example that can save useless computations). Relax, creating a  
> class does not
> spoil efficiency.

:)


>
> The second advantage of SqNumberParser is that it is a good  
> starting point for
> those wanting their own extension for Number parsing as suggested  
> by Diego
> and Francisco.
> The class could easily have other extensions like indicating the  
> error in the
> TextEditor, like for code...
>
> So far, i suppressed the bugs i know, and the class read Integer  
> ScaledDecimal
> and Float (including nan infinity and negative zero), and pass all the
> NumberParsingTests successfully.
>
> However i still have the bellerophon algorithm (from tony's  
> reference) failing
> with last bit incorrect in a few cases (i can check that with  
> asTrueFraction
> for number greater than 1.0 timestwoPower: 53). So i do not release  
> it yet.
>
> Float>>absPrintOnExactly;base: works rather well and is a good  
> candidate for
> testing readFrom:. However, i do not over trust it, because it  
> includes the
> same code that bugged absPrintOn: (See note in
> http://bugs.impara.de/view.php?id=3493) : it adds 1 to a digit without
> testing if digit=(base-9)... I did not reveal the bug yet.
>
> Nicolas
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

stéphane ducasse-2
In reply to this post by Nicolas Cellier-3
>
> I added the bug report and a few test cases at
> http://bugs.impara.de/view.php?id=3512 last night.

thanks.

>
> I have a first implementation of SqNumberParser.
> It is more convenient to have a class than a set of messages (see  
> how many are
> necessary in Number>>readFrom:), because we can store some  
> intermediate
> results and better optimize Float reading (number of trailing  
> zeroes is an
> example that can save useless computations). Relax, creating a  
> class does not
> spoil efficiency.

:)))))))))

> The second advantage of SqNumberParser is that it is a good  
> starting point for
> those wanting their own extension for Number parsing as suggested  
> by Diego
> and Francisco.
> The class could easily have other extensions like indicating the  
> error in the
> TextEditor, like for code...
>
> So far, i suppressed the bugs i know, and the class read Integer  
> ScaledDecimal
> and Float (including nan infinity and negative zero), and pass all the
> NumberParsingTests successfully.

:)
So this is crying for new tests :)

> However i still have the bellerophon algorithm (from tony's  
> reference) failing
> with last bit incorrect in a few cases (i can check that with  
> asTrueFraction
> for number greater than 1.0 timestwoPower: 53). So i do not release  
> it yet.
>
> Float>>absPrintOnExactly;base: works rather well and is a good  
> candidate for
> testing readFrom:. However, i do not over trust it, because it  
> includes the
> same code that bugged absPrintOn: (See note in
> http://bugs.impara.de/view.php?id=3493) : it adds 1 to a digit without
> testing if digit=(base-9)... I did not reveal the bug yet.


Nicolas to help us harvesting for 3.9 could you provide us with a  
list of the ready
to harvest items.

Stef


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Nicolas Cellier-3
In reply to this post by Nicolas Cellier-3
Le Vendredi 28 Avril 2006 20:08, nicolas cellier a écrit :
> However i still have the bellerophon algorithm (from tony's reference)
> failing with last bit incorrect in a few cases (i can check that with
> asTrueFraction for number greater than 1.0 timestwoPower: 53). So i do not
> release it yet.
>
> Nicolas

Subjects developped in this (too) long email are:
 - A) Float readFrom: now round exactly :)
 - B) my solution use a new ArbitraryPrecisionFloat class :(
 - C) this class can solve other asFloat problems :)
 - D) shouldn't the readFrom: fix better be moved to asFloat fix ?

A)----------------------------------------------------------------------------

OK, now i have the Float reading working, also for gradual underflow !

This of course solve Andreas' Numerics question: reading floating point
constants,
i now have
(SqNumberParser on: '6.07710050630396597660e-11') nextNumber hex =
'3DD0B4611A600000'.

B)----------------------------------------------------------------------------

The solution is not as light as i wanted, because i finally added an
ArbitraryPrecisionFloat class to handle 64bits arithmetic involved in
Bellerophon.
So i do not know if i should publish my solution at
http://bugs.impara.de/view.php?id=3512, or rather in SM.
Unfortunately my SM account is blocked inactive...

It's funny how easy creating a new Number class was.
Of course, ArbitraryPrecisionFloat lacks a short literal form syntax, it does
not implement usual mathematical functions (default is to convert to Float).
And it's not optimized at all (please don't compare to hardwired Float
arithmetic...).
But it's more general than just reading Float. This is why i wanted to publish
on SM.

C)----------------------------------------------------------------------------

This can potentially solve other asFloat incorrect rounding behaviour:

C.1) current implementation of LargeInteger>>asFloat.
will not round to nearest even Float when highBit > 53.

example:
 16r1FFFFFFFFFFFF081 asFloat hex

The ArbitraryPrecisionFloat implements IEEE rounding mode, so it can cure this
one easily.

C.2) it is more difficult with Fraction>>asFloat
which does neither round to nearest even Float...

Bellerophon and ArbitraryPrecisionFloat together can fix the Fraction problem
(conversion from decimal to binary representation is just a special case of
these Large fractions).

I now know why i never succedeed in implementing my own Smalltalk Float
reading in VW, despite of Integer infinite precision... I was over-trusting
asFloat implementation that suffer the same problem...

D)----------------------------------------------------------------------------

So, maybe we should better move the fixes i wrote from Number>>readFrom: to
LargeInteger and Fraction>>asFloat,
Then exactly rounded Float>>readFrom: will be a side effect...

Nicolas



Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Milan Zimmermann-2
Nicolas,

This looks great, thanks for your description here. (and fwiw, I'd say yes to
puting it on Impara and also yes to your D) item)

Milan
On 2006 April 30 18:38, nicolas cellier wrote:

> Le Vendredi 28 Avril 2006 20:08, nicolas cellier a écrit :
> > However i still have the bellerophon algorithm (from tony's reference)
> > failing with last bit incorrect in a few cases (i can check that with
> > asTrueFraction for number greater than 1.0 timestwoPower: 53). So i do
> > not release it yet.
> >
> > Nicolas
>
> Subjects developped in this (too) long email are:
>  - A) Float readFrom: now round exactly :)
>  - B) my solution use a new ArbitraryPrecisionFloat class :(
>  - C) this class can solve other asFloat problems :)
>  - D) shouldn't the readFrom: fix better be moved to asFloat fix ?
>
> A)-------------------------------------------------------------------------
>---
>
> OK, now i have the Float reading working, also for gradual underflow !
>
> This of course solve Andreas' Numerics question: reading floating point
> constants,
> i now have
> (SqNumberParser on: '6.07710050630396597660e-11') nextNumber hex =
> '3DD0B4611A600000'.
>
> B)-------------------------------------------------------------------------
>---
>
> The solution is not as light as i wanted, because i finally added an
> ArbitraryPrecisionFloat class to handle 64bits arithmetic involved in
> Bellerophon.
> So i do not know if i should publish my solution at
> http://bugs.impara.de/view.php?id=3512, or rather in SM.
> Unfortunately my SM account is blocked inactive...
>
> It's funny how easy creating a new Number class was.
> Of course, ArbitraryPrecisionFloat lacks a short literal form syntax, it
> does not implement usual mathematical functions (default is to convert to
> Float). And it's not optimized at all (please don't compare to hardwired
> Float arithmetic...).
> But it's more general than just reading Float. This is why i wanted to
> publish on SM.
>
> C)-------------------------------------------------------------------------
>---
>
> This can potentially solve other asFloat incorrect rounding behaviour:
>
> C.1) current implementation of LargeInteger>>asFloat.
> will not round to nearest even Float when highBit > 53.
>
> example:
>  16r1FFFFFFFFFFFF081 asFloat hex
>
> The ArbitraryPrecisionFloat implements IEEE rounding mode, so it can cure
> this one easily.
>
> C.2) it is more difficult with Fraction>>asFloat
> which does neither round to nearest even Float...
>
> Bellerophon and ArbitraryPrecisionFloat together can fix the Fraction
> problem (conversion from decimal to binary representation is just a special
> case of these Large fractions).
>
> I now know why i never succedeed in implementing my own Smalltalk Float
> reading in VW, despite of Integer infinite precision... I was over-trusting
> asFloat implementation that suffer the same problem...
>
> D)-------------------------------------------------------------------------
>---
>
> So, maybe we should better move the fixes i wrote from Number>>readFrom: to
> LargeInteger and Fraction>>asFloat,
> Then exactly rounded Float>>readFrom: will be a side effect...
>
> Nicolas

Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Andreas.Raab
In reply to this post by francisco.j.garau
Hi Francisco -

The reason I call this overkill is that the problem we've been talking
about (Number readFrom: '' readStream) can *literally* be addressed with
a one-line fix, like here:

   aStream atEnd ifTrue:[^self error: 'At least one digit expected'].

If you need a whole class to do that, I call this overkill ;-)

Cheers,
   - Andreas

[hidden email] wrote:

> Andreas,
>
> I completely agree with what you said except for the first sentence. Why
> do you think it is a "complete and utter overkill"? I can think of three
> possibilities:
>
> 1) The extra class you are adding to the image (SqNumberParser)
> 2) The extra object involved in the parsing
> 3) All the explicit methods that you have to define on the SqNumberParser
> object.
>
> None of them seems an overkill to me. I am obviously missing something.
>
> Cheers,
> Francisco
>
>
> This communication is for informational purposes only. It is not intended
> as an offer or solicitation for the purchase or sale of any financial
> instrument or as an official confirmation of any transaction. All market prices,
> data and other information are not warranted as to completeness or accuracy and
> are subject to change without notice. Any comments or statements made herein
> do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
> and affiliates.
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Andreas.Raab
In reply to this post by Nicolas Cellier-3
Hi Nicolas,

The first step is to publish what you have in any place you have access
to (or even via email) so we can test these changes and see if they work
as advertised ;-) I'm looking forward to throw a few of my test cases at
the parser and see if they come out correctly.

Cheers,
   - Andreas

nicolas cellier wrote:

> Le Vendredi 28 Avril 2006 20:08, nicolas cellier a écrit :
>> However i still have the bellerophon algorithm (from tony's reference)
>> failing with last bit incorrect in a few cases (i can check that with
>> asTrueFraction for number greater than 1.0 timestwoPower: 53). So i do not
>> release it yet.
>>
>> Nicolas
>
> Subjects developped in this (too) long email are:
>  - A) Float readFrom: now round exactly :)
>  - B) my solution use a new ArbitraryPrecisionFloat class :(
>  - C) this class can solve other asFloat problems :)
>  - D) shouldn't the readFrom: fix better be moved to asFloat fix ?
>
> A)----------------------------------------------------------------------------
>
> OK, now i have the Float reading working, also for gradual underflow !
>
> This of course solve Andreas' Numerics question: reading floating point
> constants,
> i now have
> (SqNumberParser on: '6.07710050630396597660e-11') nextNumber hex =
> '3DD0B4611A600000'.
>
> B)----------------------------------------------------------------------------
>
> The solution is not as light as i wanted, because i finally added an
> ArbitraryPrecisionFloat class to handle 64bits arithmetic involved in
> Bellerophon.
> So i do not know if i should publish my solution at
> http://bugs.impara.de/view.php?id=3512, or rather in SM.
> Unfortunately my SM account is blocked inactive...
>
> It's funny how easy creating a new Number class was.
> Of course, ArbitraryPrecisionFloat lacks a short literal form syntax, it does
> not implement usual mathematical functions (default is to convert to Float).
> And it's not optimized at all (please don't compare to hardwired Float
> arithmetic...).
> But it's more general than just reading Float. This is why i wanted to publish
> on SM.
>
> C)----------------------------------------------------------------------------
>
> This can potentially solve other asFloat incorrect rounding behaviour:
>
> C.1) current implementation of LargeInteger>>asFloat.
> will not round to nearest even Float when highBit > 53.
>
> example:
>  16r1FFFFFFFFFFFF081 asFloat hex
>
> The ArbitraryPrecisionFloat implements IEEE rounding mode, so it can cure this
> one easily.
>
> C.2) it is more difficult with Fraction>>asFloat
> which does neither round to nearest even Float...
>
> Bellerophon and ArbitraryPrecisionFloat together can fix the Fraction problem
> (conversion from decimal to binary representation is just a special case of
> these Large fractions).
>
> I now know why i never succedeed in implementing my own Smalltalk Float
> reading in VW, despite of Integer infinite precision... I was over-trusting
> asFloat implementation that suffer the same problem...
>
> D)----------------------------------------------------------------------------
>
> So, maybe we should better move the fixes i wrote from Number>>readFrom: to
> LargeInteger and Fraction>>asFloat,
> Then exactly rounded Float>>readFrom: will be a side effect...
>
> Nicolas
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fun with Number readFrom: What should we do ?

Nicolas Cellier-3
Le Lundi 01 Mai 2006 21:31, Andreas Raab a écrit :
> Hi Nicolas,
>
> The first step is to publish what you have in any place you have access
> to (or even via email) so we can test these changes and see if they work
> as advertised ;-) I'm looking forward to throw a few of my test cases at
> the parser and see if they come out correctly.
>
> Cheers,
>    - Andreas

OK i simply attach the prototype in this e-mail.

There is still work and cleaning needing to be done.
I did not create the NumberParserException,
some methods are unused and some redundant.
Proper comments are sometimes lacking.
ArbitraryPrecisionFloat needs more TestCase.

Major unfinished work lie in failure cases of Bellerophon: it will halt before
using AlgorithmR, which is not tested yet. Let me know if you encounter the
halt, i didn't yet.

SqNumberParserTest is a copy of NumberParsingTest for the interim (i did not
overide Number>>readFrom: yet).

No method is overriden by the changeSet.

You can give it a try and perhaps fix some holes i did miss in my
implementation.

Le Lundi 01 Mai 2006 20:59, Milan Zimmermann a écrit :
> Nicolas,
>
> This looks great, thanks for your description here. (and fwiw, I'd say yes
> to puting it on Impara and also yes to your D) item)

I am in favour of D) also, but did nothing about it yet...

Nicolas





Kernel-Number-ArbitraryPrecisionFloat.1.cs.gz (6K) Download Attachment
Kernel-Number-NumberParser.1.cs.gz (7K) Download Attachment
12