str2float() issue

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

str2float() issue

vstenbock
str2float('0x1') returns 1 ( as expected )
but
str2float('0b1') returns 0 ( why ? )

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

Wolf Bogacz
See help for str2float() - all "text after the number is silently ignored" for the second case.
See help in the Variables section 1.1, under the section for conversion.
For the first case; octal conversion happened in the first case before applying the str2float().


On Sun, Nov 25, 2018 at 12:51 AM <[hidden email]> wrote:
str2float('0x1') returns 1 ( as expected )
but
str2float('0b1') returns 0 ( why ? )

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

Richard Mitchell
On Sunday, November 25, 2018 at 8:41:56 AM UTC-5, Wolf wrote:

> See help for str2float() - all
> "text after the number is silently ignored" for the second case.
>
> See help in the Variables section 1.1, under the section for conversion.
> For the first case; octal conversion happened in the first case before applying the str2float().
>
>
>
>
>
> On Sun, Nov 25, 2018 at 12:51 AM <[hidden email]> wrote:
> str2float('0x1') returns 1 ( as expected )
>
> but
>
> str2float('0b1') returns 0 ( why ? )
>
 
I would have naively assumed the 'b' stood for binary, just as the 'x' stood for hex in the first example.  As such, I would have expected both to return 1, just as the OP did.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

Tony Mechelynck
In reply to this post by Wolf Bogacz
Hexadecimal (not octal) seems to be a special case:

    :echo str2float('0x1') str2float('0xff') str2float('010') str2float('0b1')

answers

    1.0 255.0 10.0 0.0

while

    :echo 0x1 0xff 010 0b1

answers

    1 255 8 1

Not yet sure if I'll call it a feature or a bug.

Best regards,
Tony.
On Sun, Nov 25, 2018 at 2:41 PM Wolf Bogacz <[hidden email]> wrote:

>
> See help for str2float() - all "text after the number is silently ignored" for the second case.
> See help in the Variables section 1.1, under the section for conversion.
> For the first case; octal conversion happened in the first case before applying the str2float().
>
>
> On Sun, Nov 25, 2018 at 12:51 AM <[hidden email]> wrote:
>>
>> str2float('0x1') returns 1 ( as expected )
>> but
>> str2float('0b1') returns 0 ( why ? )
>>
>> --
>> --
>> You received this message from the "vim_use" maillist.
>> Do not top-post! Type your reply below the text you are replying to.
>> For more information, visit http://www.vim.org/maillist.php
>>
>> ---
>> You received this message because you are subscribed to the Google Groups "vim_use" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

tyru-2
In reply to this post by vstenbock
2018年11月25日(日) 14:51 <[hidden email]>:
>
> str2float('0x1') returns 1 ( as expected )
> but
> str2float('0b1') returns 0 ( why ? )

If you want to convert strings of hexadecimal (0xa), octal (0755),
binary (0b11110000) to integers, you can use unary + operator.

:echo +'0xa'
=> 10
:echo +'0755'
=> 493
:echo +'0b11110000'
=> 240

P.S.
Vim script can treat an exponential notation, but unary + operator can
not convert it.

:echo 1.0e3
=> 1000.0
:echo +'1.0e3'
=> 1

>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.



--
Takuya Fujiwara

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

vstenbock

> If you want to convert strings of hexadecimal (0xa), octal (0755),
> binary (0b11110000) to integers, you can use unary + operator.
>
> :echo +'0xa'
> => 10
> :echo +'0755'
> => 493
> :echo +'0b11110000'
> => 240
>
> P.S.
> Vim script can treat an exponential notation, but unary + operator can
> not convert it.
>
> :echo 1.0e3
> => 1000.0
> :echo +'1.0e3'
> => 1
>

Yes, you exactly describe my pain. ))

 So my 'prefix'-dream is:  0x (hexadecimal, base 16), 0o (octal, base 8),  0b (binary, base 2)

 echo str2float('011') str2float('0x11') str2float('011e1') str2float('0b11') str2float('0o11')

and get:

11.0 17.0 110.0 3.0 9.0

instead of:

11.0 17.0 110.0 0.0 0.0

in present time

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: str2float() issue

Andy Wokula
Am 26.11.2018 um 14:38 schrieb Vladimir Stenbock:

>
>> If you want to convert strings of hexadecimal (0xa), octal (0755),
>> binary (0b11110000) to integers, you can use unary + operator.
>>
>> :echo +'0xa'
>> => 10
>> :echo +'0755'
>> => 493
>> :echo +'0b11110000'
>> => 240
>>
>> P.S.
>> Vim script can treat an exponential notation, but unary + operator can
>> not convert it.
>>
>> :echo 1.0e3
>> => 1000.0
>> :echo +'1.0e3'
>> => 1
>>
>
> Yes, you exactly describe my pain. ))
>
>   So my 'prefix'-dream is:  0x (hexadecimal, base 16), 0o (octal, base 8),  0b (binary, base 2)
>
>   echo str2float('011') str2float('0x11') str2float('011e1') str2float('0b11') str2float('0o11')
> and get:
>
> 11.0 17.0 110.0 3.0 9.0
>
> instead of:
>
> 11.0 17.0 110.0 0.0 0.0
>
> in present time

Maybe use eval() to "normalize" the input string (given you can make sure that {str} isn't arbitrary user input)?

func! Str2float(str)
     return str2float(string(eval(a:str)))
endfunc

But it's less permissive than str2float() for usual floating point numbers, eg '1e5' is not accepted.

--
Andy

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.