About str2double() and exponent identifier

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

About str2double() and exponent identifier

José Luis García Pallero
Hello,

I've noted that the function str2double() only works with exponent
identifier as 'e' or 'E', but not with 'd' or 'D', that are common
identifiers in files generated using Fortran. Then

str2double('1e2') is 100, but
str2double('1d2') is NaN

This behavior is the same in Octave as well as Matlab. Exists any
technical reason in order to not consider the identifiers 'd' or 'D'
for the conversion of the reason of the Octave's behavior is for
matching the Matlab's one?

I find str2double() more convenient in a task than str2num() as the
first one is faster, but my dataset uses 'D' as exponent identifier

Thanks
--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************

Reply | Threaded
Open this post in threaded view
|

Re: About str2double() and exponent identifier

Ian McCallion
On Fri, 10 Apr 2020 at 01:47, José Luis García Pallero <[hidden email]> wrote:
Hello,

I've noted that the function str2double() only works with exponent
identifier as 'e' or 'E', but not with 'd' or 'D', that are common
identifiers in files generated using Fortran. Then

str2double('1e2') is 100, but
str2double('1d2') is NaN

This behavior is the same in Octave as well as Matlab. Exists any
technical reason in order to not consider the identifiers 'd' or 'D'
for the conversion of the reason of the Octave's behavior is for
matching the Matlab's one?

I find str2double() more convenient in a task than str2num() as the
first one is faster, but my dataset uses 'D' as exponent identifier

str2double is a compiled function, but str2num uses eval which will be a lot slower as you say.

You would almost certainly find it faster to first replace 'd' with 'e'. e.g.:

function d= mystr2double(d)
   d(d == 'd')='e';
   d = str2double(d);
endfunction

Cheers... Ian
Reply | Threaded
Open this post in threaded view
|

Re: About str2double() and exponent identifier

Anthony Tedde

Hello,


It seems to me that there is an inconsistency, especially because the documentation of the str2double function mentions :

<<

@code{str2double} can replace @code{str2num}, and it avoids the security risk of using @code{eval} on unknown data.

>>


The issue occurs because c++ does not understand the char 'd' as the exponent 'e' when converting an istringstream to a double by using the overloaded operator>>.


This inconsistency could be fixed by inserting the following lines after the line 464 in liboctave/util/oct-string.cc:


std::size_t dpos = str.rfind('d');

if( dpos != std::string::npos){

  str.replace (dpos, 1, 1, 'e');

}


Could it be an acceptable solution to a feature request ? 

Anthony


On Fri, 10 Apr 2020 at 08:05, Ian McCallion <[hidden email]> wrote:
On Fri, 10 Apr 2020 at 01:47, José Luis García Pallero <[hidden email]> wrote:
Hello,

I've noted that the function str2double() only works with exponent
identifier as 'e' or 'E', but not with 'd' or 'D', that are common
identifiers in files generated using Fortran. Then

str2double('1e2') is 100, but
str2double('1d2') is NaN

This behavior is the same in Octave as well as Matlab. Exists any
technical reason in order to not consider the identifiers 'd' or 'D'
for the conversion of the reason of the Octave's behavior is for
matching the Matlab's one?

I find str2double() more convenient in a task than str2num() as the
first one is faster, but my dataset uses 'D' as exponent identifier

str2double is a compiled function, but str2num uses eval which will be a lot slower as you say.

You would almost certainly find it faster to first replace 'd' with 'e'. e.g.:

function d= mystr2double(d)
   d(d == 'd')='e';
   d = str2double(d);
endfunction

Cheers... Ian
Reply | Threaded
Open this post in threaded view
|

Re: About str2double() and exponent identifier

José Luis García Pallero
El vie., 10 abr. 2020 a las 13:40, Anthony Tedde
(<[hidden email]>) escribió:

>
> Hello,
>
>
> It seems to me that there is an inconsistency, especially because the documentation of the str2double function mentions :
>
> <<
>
> @code{str2double} can replace @code{str2num}, and it avoids the security risk of using @code{eval} on unknown data.
>
> >>
>
>
> The issue occurs because c++ does not understand the char 'd' as the exponent 'e' when converting an istringstream to a double by using the overloaded operator>>.

Yes, the function strtod() in C has the same behavior: only
understands 'e', and 'E' as exponent indicator

>
>
> This inconsistency could be fixed by inserting the following lines after the line 464 in liboctave/util/oct-string.cc:
>
>
> std::size_t dpos = str.rfind('d');
>
> if( dpos != std::string::npos){
>
>   str.replace (dpos, 1, 1, 'e');
>
> }

I think it should be also tested the possibility of 'D' (uppercase) as
identifier, as it is also common in Fortran as the lowercase version
'd'

>
>
> Could it be an acceptable solution to a feature request ?
>
> Anthony
>
>
> On Fri, 10 Apr 2020 at 08:05, Ian McCallion <[hidden email]> wrote:
>>
>> On Fri, 10 Apr 2020 at 01:47, José Luis García Pallero <[hidden email]> wrote:
>>>
>>> Hello,
>>>
>>> I've noted that the function str2double() only works with exponent
>>> identifier as 'e' or 'E', but not with 'd' or 'D', that are common
>>> identifiers in files generated using Fortran. Then
>>>
>>> str2double('1e2') is 100, but
>>> str2double('1d2') is NaN
>>>
>>> This behavior is the same in Octave as well as Matlab. Exists any
>>> technical reason in order to not consider the identifiers 'd' or 'D'
>>> for the conversion of the reason of the Octave's behavior is for
>>> matching the Matlab's one?
>>>
>>> I find str2double() more convenient in a task than str2num() as the
>>> first one is faster, but my dataset uses 'D' as exponent identifier
>>
>>
>> str2double is a compiled function, but str2num uses eval which will be a lot slower as you say.
>>
>> You would almost certainly find it faster to first replace 'd' with 'e'. e.g.:
>>
>> function d= mystr2double(d)
>>    d(d == 'd')='e';
>>    d = str2double(d);
>> endfunction
>>
>> Cheers... Ian



--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************