-6
1
Write a bullet-proof "Atof"
Atof
(Array to float) is a function that every language has to have. (K&R: Atof
in chapter 4.2 and strtod()
in appendix B.5.) It gets one float from one string.
For the purposes of this challenge, the number formats you must recognise are:
[spaces][-|+][digits].digits
[spaces][-|+]digits[.digits]
[spaces][-|+]digits[.]
Where [stuff]
means that stuff
may appear or not.
Input
The challenge is to write an Atof
function that takes the following two arguments:
The start index of the first character to be included in the float. This may be 0 or 1 indexed but must be consistent with the output.
The string to parse for a float. This will be printable ASCII only.
Output
You must always returns two numbers (any fitting datatype will do):
The float retrieved from the string. A whole number may optionally be returned as integer.
The index where the function stopped parsing the string to get the float, i.e. one beyond the index of the float's last character. If the float terminates the string, this will be beyond the string. Though an index is an integer, this may optionally be returned as a float. This may be 0 or 1 indexed but must be consistent with the input.
Bullet-proofing
Since your code must be bullet-proof, you must handle all the following error conditions:
Overflow: For languages with fixed precision, this means that the found float's integer part cannot fit in your language's float type. If your language has arbitrary precision, overflow means that the found float had more than 3000 digits in the integer part. If you language has more than one float type, you may chose a target type.
An input string that does not contain a valid float beginning at the start index.
A start index outside the bounds of the string.
The two arguments are not of type string (for the string) and number (for the index)
In all these cases you must still return two values which must always be the same. They must also be recognisable as indicating an error, so no valid inputs may cause the same output as the error-indicating output. Here are some examples of valid error indicators in the format float
index
:
0
"E"
""
""
-1
-1
404
0.5
0+1i
0+1i
"none"
"error"
0J1
0J1
Test cases
1-indexed, and using -1
-1
as error indicator:
1
".123"
→ 0.123
5
1
"3."
→ 3
3
1
"123/a"
→ 123
4
1
"12212.8989"
→ 12212.8989
11
3
" 12212.8989999999999922222222222222222222222"
→ 12212.899
47
9
"123456789012345.89"
→ 9012345.89
19
1
" 123456789012345.89"
→ 1.23456789E14
22
1
" 1234567890123459999.89"
→ -1
-1
1
" 123.."
→ 123
7
1
" -123.."
→ -123
7
1
" -123.999"
→ -123.999
10
1
"a123"
→ -1
-1
1
"
a123"
→ -1
-1
0
"123"
→ -1
-1
4
"123"
→ -1
-1
3
9000
→ -1
-1
"123"
1
→ -1
-1
"123"
"29393"
→ -1
-1
1
" a123.."
→ -1
-1
Example output from an APL implementation, using 1-based indexing and 0J1 0J1
(the complex variable i) for the error
Atf 1 '123'
123 4
Atf 1 '.123'
0.123 5
Atf 1 '3.'
3 3
Atf 1 '12212.8989'
12212.8989 11
Atf 3 ' 12212.8989999999999922222222222222222222222'
12212.899 50
Atf 9 '123456789012345.89'
9012345.89 19
Atf 1 ' 123456789012345.89'
1.23456789E14 22
Atf 1 '12345678901234567.89'
0J1 0J1
Atf 1 '1234567890123456.89'
1.23456789E15 20
Atf 1 ' -123..'
¯123 7
Atf 1 ' -.999'
¯0.999 7
Atf 1 ' -1.'
¯1 5
Atf 1 '1.'
1 3
Atf 1 ' -9'
¯9 4
Atf 1 'a123'
0J1 0J1
Atf 0 '123'
0J1 0J1
Atf 4 '123'
0J1 0J1
Atf 3 9000
0J1 0J1
Atf '123' 1
0J1 0J1
Atf '123' '29393'
0J1 0J1
Atf '123', ⊂2 3⍴⍳10
0J1 0J1
Atf 1 ' a123..'
0J1 0J1
Atf 1 '.'
0J1 0J1
Atf 1 ''
0J1 0J1
Atf 1 '-'
0J1 0J1
Atf 1 '+.'
0J1 0J1
Atf 1 '+.3'
0.3 4
Atf 1 '-.3'
¯0.3 4
Atf 1 '*.3'
0J1 0J1
Atf 1 ' +'
0J1 0J1
Atf 1 ' +4.'
4 5
Atf 1 ' +4.23'
4.23 7
Atf 1 '+0.'
0 4
Atf 1 '-0.'
0 4
Atf 1 '-+4.23'
0J1 0J1
Atf 1 '-+.23'
0J1 0J1
Atf 1 '-+1'
0J1 0J1
Winning
This is code-golf and the winner will be the one who can write that function in the fewest bytes.
"has to have" sounds pessimal English to me. Was it stated ipsis verbis like that in the book? – sergiol – 2018-01-25T17:50:49.080
@DLosc "abc123" has to return -1 +1 – RosLuP – 2018-01-25T19:24:34.393
@DLosc Atof 1 "abc123" has to return error : -1 -1 – RosLuP – 2018-01-25T19:40:09.333
you must handle all the following error conditions: [...] The two arguments are not of type string (for the string) and number (for the index)
How should strictly typed languages handle this requirement? – Laikoni – 2018-01-25T19:54:16.413Does the submission have to be a function, or can it be a full program? And if output can go to stdout, what determines "returning two values"? (For instance: for my error output, I print
-1
and claim that this represents the two values-
and1
.) – DLosc – 2018-01-25T20:22:19.870What's keeping you from using something like
NaN
to indicate an error? The examples you give aren't even all floats... – Sanchises – 2018-01-25T20:50:35.330@Laikoni for languages with strict type sys has nothing to do for arguments types...in fact in the type languages I seen: Function not compile if type of argument of the function not fit with function prototype – RosLuP – 2018-01-25T21:00:02.503
@DLosc it has to be a function with 2 argument in input (int string) 2 argument out (int or float + int) – RosLuP – 2018-01-25T21:04:26.263
@Sanchises Nan is enough for the error case but the float number 1.23 is not enough because one need too the index where Atof end its read – RosLuP – 2018-01-25T21:09:29.880
What? I'm asking why you suggest "none" "none" as an error output (which aren't even floats) but do not suggest something like NaN 0 which would be way more logical. Just a suggestion. – Sanchises – 2018-01-25T21:22:03.487
@Sanchises is ok NAN 0 in case of error – RosLuP – 2018-01-25T22:07:34.107
Why doesn't
1
" a123.."
→9
123
? – Adám – 2018-01-25T22:09:00.590@Adám because read the space read a, a is not a digit so it stop and return error – RosLuP – 2018-01-25T22:14:24.407
3"The index where the function stopped parsing the string to get the float, i.e. one beyond the index of the float's last character" you are asking to write
strtod
, notatof
. – Matteo Italia – 2018-01-25T22:33:22.883@MatteoItalia Ah, that's why it references
strtod()
in appendix B.5. I had been wondering. Thanks! – Adám – 2018-01-25T22:45:51.6779@RosLuP You shouldn't drastically change the spec for no reason (adding
+
) five hours after posting the challenge. It doesn't matter that the spec isn't in accordance with the book: For the purposes of this challenge, the number formats (...) are means that the book is only an inspiration and not a specification. – Adám – 2018-01-26T00:42:14.477