Hi,
Why '(1 / 2)' asNumber => 0 ? while (1 / 2) printString => '(1 / 2)' What is the standard way to convert numbers from string and reverse ? I Thought it was with printString and asNumber but it doesn't work. Vincent _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Interestingly
Object readFromString: '(1/2)' => (1 / 2) Object readFromString: '1/2' => (1 / 2) Number readFromString: '(1/2)' => 0 Number readFromString: '1/2' => 1 Fraction readFromString: '(1/2)' => 0 Fraction readFromString: '1/2' => 1 Georg Georg Heeg eK, Dortmund und Köthen, HR Dortmund A 12812 Wallstraße 22, 06366 Köthen Tel. +49-3496-214328, Fax +49-3496-214712 -----Ursprüngliche Nachricht----- Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Vincent Lesbros Gesendet: Dienstag, 19. März 2013 11:38 An: [hidden email] Betreff: [vwnc] Fraction is not a Number ? Hi, Why '(1 / 2)' asNumber => 0 ? while (1 / 2) printString => '(1 / 2)' What is the standard way to convert numbers from string and reverse ? I Thought it was with printString and asNumber but it doesn't work. Vincent _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
In reply to this post by Vincent Lesbros-2
My question is what was the justification for breaking the standrad math or was it an unintentional bug? Kind Regards Sean Glazier
Light yourself on Fire with enthusiasm and people will come for miles to watch you burn! On Tue, Mar 19, 2013 at 11:38 AM, Vincent Lesbros <[hidden email]> wrote: Hi, _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
In reply to this post by Georg Heeg
This is a good point. Maybe some explanations help to clear the
confusion.
The method CharacterArray>>asNumber is more like a scanning utility to return the next literal number in the string (a Fraction is not literal). It tries to scan a number without considering that the contents of the string could be compiled and evaluated to something that is a number. Effectively, the scanning expects an optional negative sign, then digits, then optional exponent notation (e.g. '-1e5' asNumber => 1.0e5). Even an optional plus sign is not supported, although '+1' seems so clearly a good number. That's the reason why #asNumber gives up as soon as it sees the first character in '(1 / 2)' and return zero. Even for the simple '(1)' #asNumber returns zero. The same explanation helps for #readFromString:. If the receiver is Number or any of its subclasses, then the concrete implementation delegates to Number class>>readFrom: and Number class>>readSmalltalkSyntaxFrom: which is effectively the "same" what #asNumber does. The example '1/2' returns one because the #asNumber scan stops at the '/', since it's neither minus nor digit nor exponent. The difference between Object class>>readFromString: and Number class>>readFromString: is that Object class uses the Compiler for a full translation and evaluation of the given string. The standard for converting number IS Number class>>readFromString: or #readFrom:. You can also look what PrintConverter>>convertToNumber: does. But all those mechanisms handle literal numbers only, not Fraction. btw: It's a good idea to browse through number printing protocol because there are differences in printing numbers for users or for program internal purposes. For instance the FixedPoint 1.0 has #printString = '1.0s' and for users you might want '1.00'. Regards Holger Guhl -- Senior Consultant * Certified Scrum Master * [hidden email] Tel: +49 231 9 75 99 21 * Fax: +49 231 9 75 99 20 Georg Heeg eK Dortmund Handelsregister: Amtsgericht Dortmund A 12812 Am 19.03.2013 12:10, schrieb Georg Heeg: Interestingly Object readFromString: '(1/2)' => (1 / 2) Object readFromString: '1/2' => (1 / 2) Number readFromString: '(1/2)' => 0 Number readFromString: '1/2' => 1 Fraction readFromString: '(1/2)' => 0 Fraction readFromString: '1/2' => 1 Georg Georg Heeg eK, Dortmund und Köthen, HR Dortmund A 12812 Wallstraße 22, 06366 Köthen Tel. +49-3496-214328, Fax +49-3496-214712 -----Ursprüngliche Nachricht----- Von: [hidden email] [[hidden email]] Im Auftrag von Vincent Lesbros Gesendet: Dienstag, 19. März 2013 11:38 An: [hidden email] Betreff: [vwnc] Fraction is not a Number ? Hi, Why '(1 / 2)' asNumber => 0 ? while (1 / 2) printString => '(1 / 2)' What is the standard way to convert numbers from string and reverse ? I Thought it was with printString and asNumber but it doesn't work. Vincent _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Free forum by Nabble | Edit this page |