473,888 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Unit Scalar

In one of the applications I am developing I have need of working with
numbers in varying unit system. I might be adding 5 mil to 1 inch or
whatever.

What I have come up with works really well, however...it moves about
as fast as a honda civic trying to tow a semi truck.

The basic definition looks like this:

public struct ScalarGeneric : IScalar
{
private decimal value;
private ScalarUnit unit;

// Properties, constructors, operators, etc. skipped
}

Using decimal for precision and to avoid floating point issues.

The problem essentially is this that in my operator overloads for +,
-, etc. before I can perform the actual operation I need to obviously
check what unit space my first operand is, and then convert the second
operand to match the first, then perform the operation and return the
result in the matched unit space.

So what I end up with is, is something like this:

public static ScalarGeneric operator + (ScalarGeneric a, IScalar b)
{
switch(a.unit)
{
case ScalarUnit.Cent imeter:
return new ScalarGeneric(a .Value +
b.ToCentimeters ().Value, ScalarUnit.Cent imeter); case
ScalarUnit.Inch :
return new ScalarGeneric(a .Value +
b.ToInch().Valu e, ScalarUnit.Inch );
case ScalarUnit.Mil:
return new ScalarGeneric(a .Value +
b.ToMil().Value , ScalarUnit.Mil) ;
case ScalarUnit.Mill imeter:
return new ScalarGeneric(a .Value +
b.ToMillimeters ().Value, ScalarUnit.Mill imeter);
case ScalarUnit.Degr ee:
return new ScalarGeneric(a .Value +
b.ToDegree().Va lue, ScalarUnit.Degr ee);
case ScalarUnit.Radi an:
return new ScalarGeneric(a .Value +
b.ToRadian().Va lue, ScalarUnit.Radi an);
default:
throw new System.InvalidO perationExcepti on();
}
}
And so on for other operators.

Bottom line, this works really well but isn't all that fast. I have
been trying to find ways to optimize this as even a small improvement
would make a huge difference as I can easily end up with thousands of
ops when redrawing a full scene. So far though I have been drawing a
blank on how / if I can manage to get any additional performance out
of this.

If anyone has any suggestions I would love to see them!

Thanks,

--
Stephan
2003 Yamaha R6

kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara
Jun 15 '06 #1
4 1508
> In one of the applications I am developing I have need of working with
numbers in varying unit system. I might be adding 5 mil to 1 inch or
whatever.
[snip]
The problem essentially is this that in my operator overloads for +,
-, etc. before I can perform the actual operation I need to obviously
check what unit space my first operand is, and then convert the second
operand to match the first, then perform the operation and return the
result in the matched unit space.


Using a switch block as you currently are is definitely not a nice
solution...

Instead, one method would be to always store values of the same
dimension in the same basis [e.g. always meters for lengths, always
radians for angles, etc.], and then you need to perform no [or at least
very few] conversions when doing calculations; just operate on the raw
values and set the unit of the result to match that of the first
operand. Only when you need to show the data to the user must you then
multiply by the conversion factor corresponding to the required unit.
If your system is performing lots of heavy calculation and relatively
little user interaction, this should speed things up considerably.

-- PH

Jun 16 '06 #2
Hello Paul,

You may also consider using a more generic approach. Define your class/interface
with two properties: .Value, and .Scale... Then pick an arbitrary scale
against which everything else gets scaled. Lets say that you are using US
measurements.. and that the US Inch is your basic unit of measure.. Then
an Inch would look like:

Inch
.Scale = 1
.Value = whatever

And a Foot would look like:

Foot
.Scale = 12
.Value = whatever

Now when you go to overload your operatos you can do (assume you want the
result in the first parameter's (Value1) scale):

For Addition:
return (Value1.Value / Value1.Scale) + (Value2.Value / Value2.Scale)
This method should work well as long as you group your units into logical
groups.. (ie, don't go mixing Volume measurements (cubic feet) with time
measurements (seconds).. they just wont make sense and you'll get gibberish
back)

-Boo
In one of the applications I am developing I have need of working
with
numbers in varying unit system. I might be adding 5 mil to 1 inch or
whatever.
[snip]
The problem essentially is this that in my operator overloads for +,
-, etc. before I can perform the actual operation I need to obviously
check what unit space my first operand is, and then convert the
second
operand to match the first, then perform the operation and return the
result in the matched unit space.

Using a switch block as you currently are is definitely not a nice
solution...

Instead, one method would be to always store values of the same
dimension in the same basis [e.g. always meters for lengths, always
radians for angles, etc.], and then you need to perform no [or at
least very few] conversions when doing calculations; just operate on
the raw values and set the unit of the result to match that of the
first operand. Only when you need to show the data to the user must
you then multiply by the conversion factor corresponding to the
required unit. If your system is performing lots of heavy calculation
and relatively little user interaction, this should speed things up
considerably.

Jun 16 '06 #3
On Fri, 16 Jun 2006 21:55:12 +0000 (UTC), GhostInAK
<gh*******@gmai l.com> wrote:
Hello Paul,

You may also consider using a more generic approach. Define your class/interface
with two properties: .Value, and .Scale... Then pick an arbitrary scale
against which everything else gets scaled. Lets say that you are using US
measurements .. and that the US Inch is your basic unit of measure.. Then
an Inch would look like:

Inch
.Scale = 1
.Value = whatever

And a Foot would look like:

Foot
.Scale = 12
.Value = whatever

Now when you go to overload your operatos you can do (assume you want the
result in the first parameter's (Value1) scale):

For Addition:
return (Value1.Value / Value1.Scale) + (Value2.Value / Value2.Scale)
This method should work well as long as you group your units into logical
groups.. (ie, don't go mixing Volume measurements (cubic feet) with time
measurements (seconds).. they just wont make sense and you'll get gibberish
back)

-Boo


Thanks everyone for your replies.

I really like that second idea with the scale, I think I might go that
route.
--
Stephan
2003 Yamaha R6

kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara
Jun 18 '06 #4
Hello Stephan,

I thought this sounded like a fun project so I started writing it myself
as well. I have run into a few bumps along the way.. mainly due to bad planning.
The system of a single Scale value works well for those units whose system
can be converted using a simple scaling approach. For other systems (eg,
temperature conversions) this system doesnt work so well.

Here's my overloaded addition operator for the Scale method:

'
' Will return a UnitOfMeasure in tValueOne's scale.
'
Public Shared Operator +(ByVal tValueOne As UnitOfMeasure, ByVal tValueTwo
As UnitOfMeasure) As UnitOfMeasure

Dim tReturn As UnitOfMeasure = New UnitOfMeasure

tReturn.Scale = tValueOne.Scale
tReturn.Value = ((tValueOne.Val ue * tValueOne.Scale ) + (tValueTwo.Valu e
* tValueTwo.Scale )) / tValueOne.Scale

Return tReturn

End Operator
On Fri, 16 Jun 2006 21:55:12 +0000 (UTC), GhostInAK
<gh*******@gmai l.com> wrote:
Hello Paul,

You may also consider using a more generic approach. Define your
class/interface with two properties: .Value, and .Scale... Then pick
an arbitrary scale against which everything else gets scaled. Lets
say that you are using US measurements.. and that the US Inch is your
basic unit of measure.. Then an Inch would look like:

Inch
.Scale = 1
.Value = whatever
And a Foot would look like:

Foot
.Scale = 12
.Value = whatever
Now when you go to overload your operatos you can do (assume you want
the result in the first parameter's (Value1) scale):

For Addition:
return (Value1.Value / Value1.Scale) + (Value2.Value / Value2.Scale)
This method should work well as long as you group your units into
logical groups.. (ie, don't go mixing Volume measurements (cubic
feet) with time measurements (seconds).. they just wont make sense
and you'll get gibberish back)

-Boo

Thanks everyone for your replies.

I really like that second idea with the scale, I think I might go that
route.

--
Stephan
2003 Yamaha R6
kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara

Jun 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4343
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
1
2252
by: sab | last post by:
Hi, We have a web form with a listbox. The listbox is a multiple select listbox and has data that looks something like: ALL Unit 1 Unit 2 Unit 3 Note: "ALL" is always the first item in the list.
72
5296
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
4
5768
by: beatdream | last post by:
I am designing a database to handle different kinds of products ... and these products can have different properties...for example, a trouser can be specified by the width, length, color, and other additional properties...while you might need only one property for another product, e.g litres for a soft drink...so I was thinking of creating a properties table, a units table and a product table, properties-unit table... and...
0
923
by: uymqlp502 | last post by:
Version 1.4 of my scalar class is available at http://RussP.us/scalar.htm No major changes. I have corrected the "repr" function to make it more useful, and I have added a "unit_type" function that returns the type of a unit (e.g., time, length, force). The unit_type function is intended mainly for interactive, "calculator-style" use. If you do scientific or engineering calculations or programming,
3
6254
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" ...
3
5408
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"...
2
1763
by: Chandan Kr Sah | last post by:
Currently I have approx 35000 records and for populating those records in Report I have to insert those records in a temprorary table through loop. But for this operation(insertion in temp table) its taking more than 8 min.currently I am using DAO connection.So how can I reduce the time for 1-2 min.The code is given below:- Dim Dbs As Database Dim rst As DAO.Recordset Set Dbs = CurrentDb Set rst = Dbs.OpenRecordset(ssql) ...
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
11181
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10886
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.