Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
floating problem of eval()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
oversky  
View profile  
 More options Nov 13 2012, 12:43 am
From: oversky <mail...@gmail.com>
Date: Mon, 12 Nov 2012 21:43:21 -0800 (PST)
Local: Tues, Nov 13 2012 12:43 am
Subject: floating problem of eval()
I use eval function to do some simple calculations.
Today I found that some calculations do not return correct answers.
For example,

:echo eval('1/2')
0

:echo eval('1/2.0')
0.5

:echo eval('1.0/2')
0.5

:echo eval('1.0/2.0')
0.5

I use vim from http://portableapps.com.
This version is compiled with +float.
Can anyone confirm this with your vim?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Danny Gratzer  
View profile  
 More options Nov 13 2012, 12:46 am
From: Danny Gratzer <danny.grat...@gmail.com>
Date: Mon, 12 Nov 2012 23:45:14 -0600
Local: Tues, Nov 13 2012 12:45 am
Subject: Re: floating problem of eval()

These look correct to me, when you do eval("1/2") you are doing integer
division, so it should return 0. In every other case you have a floating
point so you will be doing floating point division, so you should get .5.
Why is this strange?

--
Danny Gratzer

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
oversky  
View profile  
 More options Nov 13 2012, 1:01 am
From: oversky <mail...@gmail.com>
Date: Mon, 12 Nov 2012 22:00:46 -0800 (PST)
Local: Tues, Nov 13 2012 1:00 am
Subject: Re: floating problem of eval()
Thanks for replying.
The eval() is used in a inline calculator function.
It's annoy that I have to remember not to input integer in an
equation,
especially when the equation is pasted from somewhere else.
Is there anyway to force floating calculation?

On 11月13日, 下午1時46分, Danny Gratzer <danny.grat...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Brabandt  
View profile  
 More options Nov 13 2012, 1:29 am
From: "Christian Brabandt" <cbli...@256bit.org>
Date: Tue, 13 Nov 2012 07:28:50 +0100
Local: Tues, Nov 13 2012 1:28 am
Subject: Re: floating problem of eval()

On Tue, November 13, 2012 07:00, oversky wrote:
> Thanks for replying.
> The eval() is used in a inline calculator function.
> It's annoy that I have to remember not to input integer in an
> equation,
> especially when the equation is pasted from somewhere else.
> Is there anyway to force floating calculation?

Add 0.0 to each number.

regards,
Christian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Beckett  
View profile  
 More options Nov 13 2012, 1:46 am
From: "John Beckett" <johnb.beck...@gmail.com>
Date: Tue, 13 Nov 2012 17:45:46 +1100
Local: Tues, Nov 13 2012 1:45 am
Subject: RE: floating problem of eval()

oversky wrote:
> :echo eval('1/2')
> 0

As Danny mentioned, that is not strange as Vim follows the same
concepts as used in the C language (and others) where "1" is an
integer, but "1.0" (both without quotes) is a floating point
number.

You get the same in Python 2.7, where 1/2 also evaluates as 0.

You don't need "eval":
    :echo 1.0/2
gives
    0.5

If you have numbers as strings in variables, you could convert
them to floats like this:
    :let n = "1"
    :let d = "2"
    :echo str2float(n)/str2float(d)
which gives 0.5.

John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vlad Irnov  
View profile  
 More options Nov 13 2012, 7:55 am
From: Vlad Irnov <vlad.ir...@gmail.com>
Date: Tue, 13 Nov 2012 04:55:07 -0800 (PST)
Local: Tues, Nov 13 2012 7:55 am
Subject: Re: floating problem of eval()

In Python 2.x this is easily solved like this:
>>> 1/2
0
>>> from __future__ import division
>>> 1/2

0.5

Unfortunately, my Vim is not able to import from future:
:py print 1/2
0
:py from __future__ import division
:py print 1/2
0

The workaround I use is to have the code that does calculations in a separate Python module which imports division from future, and then import that module from Vim script. I can post my script for inline calculations with Pythons if someone is interested.

There other advantages in using Python for calculations instead of VimScript.
Integer overflow is less likely to be a problem. This is what I get:
:echo 111111111*111111111
165372529
:py print 111111111*111111111
12345678987654321

The x**y notation is more convenient than pow(x,y).

Regards,
Vlad


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »