GithubHelp home page GithubHelp logo

Comments (4)

Andreas113 avatar Andreas113 commented on August 17, 2024

All such "stupid" inputs containing spaces can be easily solved by removing all spaces and making the program more secure. The following routine is very suitable for this purpose:

{
  Deletes all occurrences of sub-String Needle from String HayStack AND returns
  the resulting String. HayStack is returned unchanged If it does NOT contain
  Needle. The search is Case sensitive.
}
Function DelAllStr(CONST Needle, Haystack: String): String;
// http://delphidabbler.com/url/csdb.
Var
  StartIdx: Integer;
Begin
  Result:= Haystack;
  StartIdx:= SysUtils.AnsiPos(Needle, Result);
  While StartIdx > 0 Do
  Begin
    Delete(Result, StartIdx, Length(Needle));
    StartIdx:= SysUtils.AnsiPos(Needle, Result);
  End;
End;

A call like this solves all these problems:

CONST
  Space = β€˜ ’;
. . .
S:= DelAllStr(Space, S);
. . .

Andreas

from neslib.multiprecision.

neslib avatar neslib commented on August 17, 2024

We are entering a bit of a grey area here. Usually, whitespace is used to separate multiple values. In that case it shouldn't be ignored. Also, entering whitespace in "normal" floating-point literals is not allowed in Delphi.

I will look into this this weekend if I have some time. In general, I want to follow the same conventions as the Delphi RTL. So if the standard StrToFloat function does not accept whitespace, then neither should the DoubleDouble/QuadDouble version.

One way to make stupid users less stupid is to not accept stupid inputsπŸ˜‰

from neslib.multiprecision.

Andreas113 avatar Andreas113 commented on August 17, 2024

OK Erik, agree with you. But then such an input as follows should not be accepted at all, because thereby an apparent result - namely a wrong one - comes out:

Y/X:
----
X            = 1E - 1
Y            = - 2
MPA_Float    = -20 ---> Correct
QuadDouble   = -2.00000000000000000000000000000000000000000000000000000000000000
DoubleDouble = -2.0000000000000000000000000000000

Thanks
Andreas

from neslib.multiprecision.

neslib avatar neslib commented on August 17, 2024

I made some changes to make the parsing behave more like Delphi's StrToFloat:

  • When using the implicit String operator (as in X := '- 2'), it will raise an EConvertError exception now when the input is invalid, so it will not create invalid output.
  • Leading and trailing whitespace is accepted (as in StrToFloat).
  • Other whitespace is regarded as a separator and stops parsing at that point. So X := '1 2' is parsed successfully as 1, but X := '- 2' results in an error because '-' by itself is not a valid number.

This will probably not account for all possible (parsing) errors, but it is more closely in line with what Delphi's StrToFloat does.

Let me know if there are any other cases where the behavior differs from the StrToFloat behavior.

from neslib.multiprecision.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.