Page 1 of 1
⎕VFI Question on Negative Number
Posted: Fri Oct 28, 2011 8:39 pm
by PGilbert
If you do in Dyalog APL the following:
⎕VFI '¯2'
1 ¯2
⎕VFI '-2'
0 0
you get that '-2' is not a correct number. Why is that ?
Oddly in APL+Win
⎕VI '-2'
will give 1, but it is stated in the documentation that
'-'
is treated as
'¯'
and in APLX
⎕VI '-2'
will return 0, like in Dyalog. Now I know that if one is using ⎕VFI to validate the character input of a user it should do something like
((text='-')/text)←'¯'
before using ⎕VFI, but again what's wrong with '-2' ?
Thanks
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 11:54 am
by DanB|Dyalog
-2, just like *2 or £2, is not a number in APL.
That's the way Dyalog (and others) implemented []VFI (or []FI).
Since this has been going on for years they can't simply change the behaviour now without seeing people's code breaking.
But if enough demand is met they may implement other features.
How do you see it working? With an extension to the left argument to specify whether - is acceptable? Using the new [:] operator?
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 12:28 pm
by PhilGray
Postby PhilGray on Sun Oct 30, 2011 8:28 am
How do you see it working? With an extension to the left argument to specify whether - is acceptable? Using the new [:] operator?
May I suggest a Return-Code of length 3 ?
RC[ 1 2 ] remaining the same, and RC [ 3 ] representing the "type" of "translation" done if the element/cell to be translated was not "conventional" .
e.g.: RC [ 3 ]
0 ... conventional translation = no 'exception' case
1 ... '-' was replaced with '¯'
2 ... a currency symbol was found and ignored
3 ... ',' was interpreted as a "." decimal-marker ( European versus American )
4 ... more than one number was found in the cell, of which only the first was used
5 ... etc.
6 ... etc.
The list could be expanded as new cases arise, thus easy to maintain.
Also, any reference to ⎕VFI in existing code could easily be "Found & Replaced" with a 2↑⎕VFI , or a cover function ( e.g. ∆∆VFI ) that does the 2↑ internally.
Just my 2 Cents.
PhilGray
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 2:06 pm
by PGilbert
Thanks Daniel for the education, this is answering my question. Since ⎕VFI can be used to validate the input text of a user that will use '-' for a negative number, I would like to suggest that a warning to that effect be added to the online help of ⎕VFI to help the newbies like me coming from another APL to port their code.
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 5:44 pm
by DanB|Dyalog
PhilGray wrote:May I suggest a Return-Code of length 3 ?
You can, of course, but any change which is not upward compatible is unlikely to be met with enthusiasm.
There is a lot of code out there like
which would break if ⎕VFI were to return a 3rd element.
The new ⍠ (Variant) operator would be an ideal choice for this.
For example we could have
to specify - and ¯ as NEGative decorators.
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 6:25 pm
by DanB|Dyalog
WRT translating strings, the new []R operator makes changing strings a trivial thing:
1 ... '-' was replaced with '¯'
2 ... a currency symbol was found and ignored
3 ... ',' was interpreted as a "." decimal-marker ( European versus American )
you can do
Code: Select all
from←'-' '[$£¤€¥]' ','
to←'¯' '' '.'
]disp ⎕VFI from ⎕r to ⊢ '3.3 -321 $32,45 -654.32¥'
┌→──────┬──────────────────────┐
│1 1 1 1│3.3 ¯321 32.45 ¯654.32│
└~─────→┴~────────────────────→┘
and even more complex expressions like single spaced numbers (e.g. 12 345,67),
numbers surrounded by parentheses to denote negative numbers (e.g. (32.56) )
and multiple character currencies (e.g. 123.45DK):
Code: Select all
from←'[-(]' '([$£¤€¥]|DK|\))' ',' '(\d) (\d)'
to←'¯' '' '.' '\1\2'
]disp ⎕VFI from ⎕r to ⊢ 'DK123 456 789,50 -65 124.32¥ (12 345 678.90)€'
┌→────┬─────────────────────────────────┐
│1 1 1│123456789.5 ¯65124.32 ¯12345678.9│
└~───→┴~───────────────────────────────→┘
The possibilities are endless.
Re: ⎕VFI Question on Negative Number
Posted: Sun Oct 30, 2011 7:55 pm
by PhilGray
Thanks Dan.
Of course we can use APL to solve these exceptions as you have shown , but my thinking was simply that having the Interpreter handle these "exceptions" at a (low) machine.code level would be ..
a) much faster in execution
b) more comfortable
Cheers
Re: ⎕VFI Question on Negative Number
Posted: Mon Oct 31, 2011 10:50 pm
by DanB|Dyalog
I understand Phil, what I was driving at was that it would be nice to have all these features in the language but
the problem is we don't have a concensus (yet) and it might take time to
a. get one
b. get Dyalog (or any other implementor) to put it in the language
So in the meantime we can experiment (APL is pretty good at that) and with the new []R operator it is even easier.
Cheers
Re: ⎕VFI Question on Negative Number
Posted: Thu Nov 03, 2011 9:47 am
by AndyS|Dyalog
I've logged an issue to amend the ⎕VFI documentation to show that numbers such as "-2" are not valid.