2

First of all, is this function special to processing or does it exist in java by default?

When I code in processing the line below,

println(float("88") + "\t" + float("88\n") + "\t" + float("88p") ); // p is just an arbitrary character other than '\n'

It outputs

88.0 88.0 NaN

So, why float() function works fine with '\n' character but does not work with 'p' ? Aren't they both characters? I know '\n' is something special but in this case does it make a difference?

edit : I have replaced 'K' with 'p' because of some warnings came from the answers.

0

3 Answers 3

5

It exists only in processing. If you wanted a similar function in Java, there's Float#parseFloat(String)

I assume it works with \n, because that's a newline character which is whitespace. float likely trims whitespace before parsing the value. K is not a whitespace character and not a digit, so it cannot be parsed to a float.

Sign up to request clarification or add additional context in comments.

8 Comments

So it is something related how float() function was written by processing team. Right? And also do you know how can I access the definition of this function or any other functions of processing? Is it open source?
Yes, their source code is available on GitHub: github.com/processing/processing
Plus, k could have actual semantic meaning - if I saw 88k in print, I'd assume that they meant 88,000 (most likely $88,000 or 88,000 of some other unit of currency). With the specific case of k, it's not clear whether float should interpret it as 88 or 88,000.
@EJoshuaS-ReinstateMonica: could have, but doesn't for this function. It would be way to difficult to parse all possible suffixes and not generating surprising results.
@knittl Yes, that's the exact point I make in my answer, actually - there are too many edge cases and ambiguities to be able to implement that behavior reasonably.
|
1

Presumably, it treats the \n as "meaningless filler" (i.e. it can be safely stripped off).

Also, in this context, k is not just an arbitrary character. 88k could be interpreted as 88,000, for example (which is not an all uncommon usage). Or, maybe that's not what you meant at all and the k was just a typo - either way, the framework doesn't know, so you're effectively asking it to read your mind here.

To give another example, consider a case like float("12abf456x"). What should the framework do with this? It's not at all clear how you could reasonably interpret that as a number with any certainty.

6 Comments

well, I guess I have chosen a risky character :D It was just arbitrary and does not mean anything, I have tried with 'p' 't' 'z' etc. Nothing differs.
@muyustan That's true :) Still, it could have cultural connotations that the library writers didn't know about (or didn't think of) when they wrote the framework (like that), in which case it's unclear whether it should be interpreted as 88 or 88,000. Point being, there are lots of edge cases where it would be really hard to interpret what was intended if you allow arbitrary characters like that, but there's no ambiguity with something like a newline character that occurs at the end of the string - you can definitely safely strip it off without changing the meaning.
Ok, so in the implementation of float() function, it has to be trimming the '\n' characters in the string before it converts it to a float, so '\n' does not lead to any problems. Right?
I haven't looked at the implementation, but I assume so, because it's guaranteed not to affect the meaning. I'm not sitting in front of Processing, but I'm curious - what happens if you put the \n in the middle of the string (like float("8\n8"))? I assume that, in that case, it would throw an error.
Yeah, that makes sense - that would seem to support the idea that that they're playing it safe and only stripping off characters when they're sure that it won't change the meaning.
|
1

That specific float function exists in Processing

In java there's Float.parseFloat(s: String): float. This function shall yield NumberFormatExceptions when passing strings that cannot be recognised as proper floats.

\n is tipically treated as blank or whitespace. Strings are tipically trimmed before parsed (that is to say, whitespace and characters like \r and \n are removed from the beginning and end of said string). The K character is not removed, hence float() yields NaN (In Java you'd get a NumberFormatException)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.