Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline btowler  
#1 Posted : 29 November 2012 03:11:03(UTC)
btowler

Rank: Newbie

Groups: Registered
Joined: 26/06/2012(UTC)
Posts: 9
United States
Location: Massachusetts

There's a problem with the Range(3) function.

Here's an example:

1. Create a vector for a variable n using the range(3) function such as:

n:=0.03, 0.0325.. 0.05

This should produce a vector with 9 elements ranging from 0.03 to 0.05 in 0.0025 increments.

However, it actually produces an 8 element vector from 0.03 to 0.0475. It appears the last element (0.05) is truncated.

Why?

Thanks in advance,
Brett
File Attachment(s):
Bug in Range(3) function, SMath 0.95.sm (6kb) downloaded 26 time(s).

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline omorr  
#2 Posted : 29 November 2012 11:50:25(UTC)
omorr


Rank: Administration

Groups: Registered, Advanced Member
Joined: 23/06/2009(UTC)
Posts: 1,740
Man
Serbia

Was thanked: 318 time(s) in 268 post(s)
Hello Brett,

I think it is due to the machine floating point rounding error. Here is the possible explanation of it.

n:range(0.03,0.05+10^{-16},0.0325)


n=mat(0.03,0.0325,0.035,0.0375,0.04,0.0425,0.045,0.0475,0.05,9,1)

Adding a rather small number to the last number (order of magnitude of computing precision ~15 decimal place) might solve this.

Regards,
Radovan

Edited by user 29 November 2012 14:09:05(UTC)  | Reason: Not specified

When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
Offline btowler  
#3 Posted : 01 December 2012 02:51:51(UTC)
btowler

Rank: Newbie

Groups: Registered
Joined: 26/06/2012(UTC)
Posts: 9
United States
Location: Massachusetts

Thanks Radovan! That worked (though I'm somewhat concerned that an error like this popped up in the first place).

Brett
Offline mkraska  
#4 Posted : 01 December 2012 03:41:26(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,988
Germany

Was thanked: 1126 time(s) in 723 post(s)
I agree with Radovan that this feature is not exactly a bug. However, testing the loop exit condition with some tolerance halfway between the iterator increment and machine precision would be a default that might perhaps meet the intent of most users. Then ugly workarounds could be avoided.
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Offline btowler  
#5 Posted : 01 December 2012 08:46:06(UTC)
btowler

Rank: Newbie

Groups: Registered
Joined: 26/06/2012(UTC)
Posts: 9
United States
Location: Massachusetts

I think the easiest (and perhaps cleaner) workaround is the alternate method I included in the attachment. By using a For loop with a dummy (integer) index variable (e.g., i) run through a range of 1 to 9, then under the For loop create the vector n in an equation using the index variable.

For i:=1..9
n:=0.03+(i-1)*0.0025
Offline mkraska  
#6 Posted : 01 December 2012 13:51:28(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,988
Germany

Was thanked: 1126 time(s) in 723 post(s)
The same without for loop:

0.03+0.0025*(range(0,8))=mat(0.03,0.0325,0.035,0.0375,0.04,0.0425,0.045,0.0475,0.05,9,1)

Both versions of the workaround obfuscate the upper loop limit, that's why I would like the range(3) function to be modified. However, the workarounds make the iterator increment more obvious. Would perhaps be fine to have an alternative range(3) with range(start,stop,step) displaying as

start..stop by step
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
thanks 1 user thanked mkraska for this useful post.
on 01/12/2012(UTC)
Offline Davide Carpi  
#7 Posted : 01 December 2012 16:09:39(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,648
Man
Italy
Location: Italy

Was thanked: 1331 time(s) in 876 post(s)
Originally Posted by: mkraska Go to Quoted Post
The same without for loop:

0.03+0.0025*(range(0,8))=mat(0.03,0.0325,0.035,0.0375,0.04,0.0425,0.045,0.0475,0.05,9,1)

Both versions of the workaround obfuscate the upper loop limit, that's why I would like the range(3) function to be modified. However, the workarounds make the iterator increment more obvious. Would perhaps be fine to have an alternative range(3) with range(start,stop,step) displaying as

start..stop by step


I agree with Martin Biggrin
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.