Page 1 of 2 12>
Topic Options
#66903 - 2002-06-13 10:56 AM Ohms Law - Math routine
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Okay.. been working with this for about an hour now and can't seem to get the correct results.

I thought it would be an easy task to do, but I'm having trouble understanding how it works I guess. Can someone point me in the right direction as to what or where I'm going wrong.

I was trying to create a simple OHMS/POWER Calculator... [Frown]

I'm using KiXtart 2001 4.10 Release Candidate 2

code:
; $v=Voltage $i=Current $r=Resistance
$v=FormatNumber("25,4")
$i=FormatNumber("2,4")
$r=FormatNumber($v,4/$i,4)
? "Voltage is: " FormatNumber($v,4)
? "Current is: " FormatNumber($i,4)
? "Resistance is: " FormatNumber($r,4) + " Ohms "

; Result = 25.0000 which is the wrong value


$v=VAL(25)
$i=VAL(2)
$r=val($v/$i)
? "Voltage is: " val($v)
? "Current is: " val($i)
? "Resistance is: " val($r) + " Ohms "

; Result = 12 which is the wrong value

Power (P), expressed in WATTS P = E*R P = E^2/R P = I^2*R
Voltage (E), expressed in VOLTS V = P/I V = I*R V = Sq root of (P*R)
Current (I), expressed in AMPERES I = E/R I = P/E I = Sq root of (P/R)
Resistance (R), expressed in OHMS R = E^2/P R = P/I^2 R = E/I

[ 13 June 2002, 10:57: Message edited by: NTDOC ]

Top
#66904 - 2002-06-13 11:09 AM Re: Ohms Law - Math routine
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
OI Doc,

what does FormatNumeber() do ???

J.

Well , 12 is at least near (should be 12,5)

[ 13 June 2002, 11:18: Message edited by: jpols ]
_________________________



Top
#66905 - 2002-06-13 11:46 AM Re: Ohms Law - Math routine
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I've not used 4.10, but don't you need to force the variable type otherwise you'll default to integer.

Try assignining the values as:
code:
$v=25.0
$i=2.0


Top
#66906 - 2002-06-14 12:11 AM Re: Ohms Law - Math routine
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
richard, yes you have to but one shouldn't need to.
this is kinda bug in the automatic variable type rutines.
_________________________
!

download KiXnet

Top
#66907 - 2002-06-14 12:31 AM Re: Ohms Law - Math routine
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No, I don't think it's a bug - it's correct behaviour.

An awful lot of scripts out there use integer math for calculation. If the default behaviour was floating point math then these existing scripts would all be broken.

Keeping the default behaviour as integer means that existing scripts will continue to work without modification. If you want the number to be treated as floating point then you have to declare it, or cast it.

Top
#66908 - 2002-06-14 12:40 AM Re: Ohms Law - Math routine
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sorry richard, but now is time to say to you: RTFM! [Big Grin]

quote:
In KiXtart, variables are always of one fundamental data type: Variant. The current implementation of KiXtart uses three types of variants: long integers, doubles (8-byte floating point numbers) and strings. A variant of type string can contain up to 32,000 characters. Integer variables can contain any value between ?2,147,483,648 and 2,147,483,647. The type of a variable is automatically changed to the result of the expression that is assigned to it. This means that if you assign a string to a variable containing an integer, the type of the variable is changed to a string.
_________________________
!

download KiXnet

Top
#66909 - 2002-06-14 12:54 AM Re: Ohms Law - Math routine
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Aha! But of course a bare number is... an integer.

So, $x=25 will result in $x being an integer, because the type of the expression "25" is an integer type.

$x=25.0 will result in $x being a double, because the expression "25.0" is a double type.

In the code
code:
$a=25
$b=2
$c=$a/$b

Will result in "12", because the variables $a and $b are both integer, so the math expression is integer.
The code
code:
$a=25.0
$b=2.0
$c=$a/$b

Will result in "12.5" because $a and $b are both doubles, and the result of the math expression is a double.

Nice try, but no cigar [Wink]

[ 13 June 2002, 12:58: Message edited by: Richard Howarth ]

Top
#66910 - 2002-06-13 01:18 PM Re: Ohms Law - Math routine
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mm...
you have point somewhat in that.

but.
kixtart counts before assigning the value.

so 3/2 is counted as 1,5 and then translated.
this behaviour is wrong.

but to use the current way of functioning one could fix the function with:
code:
$a=25
$b=2
$c=0.+$a/$b

_________________________
!

download KiXnet

Top
#66911 - 2002-06-13 01:50 PM Re: Ohms Law - Math routine
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No, it's right.

Both "3" and "2" are integers, so the result is an integer. If you don't want an integer result you must cast it, or otherwise coerce the expression type.

Here is a simple script showing the processes:
code:
$iInteger=3
$dDouble=3.0

$vVariant=3/2 "Calculation using integer expression=" $vVariant ?
$vVariant=3.0/2 "Calculation using double expression=" $vVariant ?
$vVariant=$iInteger/2 "Calculation using integer=" $vVariant ?
$vVariant=$dDouble/2 "Calculation using double=" $vVariant ?

The results are as expected:
quote:
Calculation using integer expression=1
Calculation using double expression=1.5
Calculation using integer=1
Calculation using double=1.5

Interestingly "0.0+$a/$b" won't work. Due to operator precedence, "$a/$b" is evaluated first, and as an integer expression will result in "1". Adding 0.0 to it will have no effect.

You will need to use parentheses to change the precedence so that it becomes "(0.0+$a)/$b", or use a high precedence order operator like "1.0*$a/$b".

Using "1.0 * expression" is a good standard way of casting the expression to double, as KiXtart doesn't support true casting.

Unfortunately CDBL(3/2) results in an answer of "1", which is counter-intuitive. This is because the expression itself is an integer expression and is evaluated before being passed to the CDBL() function. A better solution would have been "C" type casting. Using this the expression becomes "(cdbl) $a/$b", which forces the expression to the correct type.

[ 13 June 2002, 13:51: Message edited by: Richard Howarth ]

Top
#66912 - 2002-06-13 02:03 PM Re: Ohms Law - Math routine
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mm...
so the counting is wrong?

as it correctly assigns the right type to var the counter works incorrectly when it assumes that the result is same type as the values counted.

that my example didn't work like you said it wouldn't... said before tested.
_________________________
!

download KiXnet

Top
#66913 - 2002-06-13 02:07 PM Re: Ohms Law - Math routine
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
The problem with using FormatNumber in that way is that it doesn't assign the variable the type double, but formats it for viewing only and changes it to a string.

$i = FormatNumber("25,4") returns the string "25.00". As to how this result is obtained, I'm not sure, but it can't be used as a number again unless you do
$i = VAL($i)
....
$i = FormatNumber(25,4) returns the string
"25.0000"

So, FormatNumber does NOT work like a TRUNCATE function would work.

Example:
code:
; $v=Voltage $i=Current $r=Resistance
$v=CDBL(25)
$i=CDBL(2)
$r=FormatNumber($v/$i)
? "Voltage is: " FormatNumber($v,4)
? "Current is: " FormatNumber($i,4)
? "Resistance is: " FormatNumber($r,4) + " Ohms "
; Result = 25.0000 which is the wrong value
$v=CDBL(25)
$i=CDBL(2)
$r=$v/$i
? "Voltage is: " $v
? "Current is: " $i
? "Resistance is: " $r + " Ohms " ?
; Result = 12 which is the wrong value

This should return proper values. In this case only the first variable evaluated in an expression need be type CDBL, but i put it in anyway to avoid confusion.

Brian

Top
#66914 - 2002-06-13 02:41 PM Re: Ohms Law - Math routine
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I am wondering the following, per the manual:

quote:

Real/floating point math Floating point expressions are now supported, and various supporting functions have been added: Round, CInt, CDbl, CStr and FormatNumber

If we dig back into the MS-Access helpfile, we should also be seeing - CLNG..

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q239104

I know this is not Access, but it would kinda neat to see some of these functions..

Another example, MID..

A cool function we can use from vb is Replace..

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#66915 - 2002-06-13 05:45 PM Re: Ohms Law - Math routine
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Put this into the suggestion Forum:

VB-type Functions: CLNG, MID, REPLACE

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#66916 - 2002-06-13 06:49 PM Re: Ohms Law - Math routine
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Richard,

Thank you very much for the quite informative information on this. It would appear this little project will actually require more coding then I originally thought.

Thanks again. Nice to have a coding/math geek on board. [Wink]

Top
#66917 - 2002-06-13 07:07 PM Re: Ohms Law - Math routine
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Doc,

You mean something like:

"The 32nd vector of the 93rd helium atom travelling through the time and space equilibrium."

??

[Big Grin] [Big Grin] [Big Grin]

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#66918 - 2002-06-13 07:12 PM Re: Ohms Law - Math routine
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
never forget the Heisenberg Uncertainty Principle
http://www.aip.org/history/heisenberg/
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#66919 - 2002-06-13 09:21 PM Re: Ohms Law - Math routine
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Rad,

You are digging back in to the days of my college Physics Courses. My brain hurts!! [Wink]

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#66920 - 2002-06-14 12:54 AM Re: Ohms Law - Math routine
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Oh, come on... it can't be much harder then this...

p'x + (h sinA ) / L' = p''x - (h sinA ) / L' (n = 1..) 1/n(2k) = (-1)(k-1) PI(2k) 2(2k) B(2k) / ( 2(2k)!)

Now how do I convert this to KiXtart?... [Big Grin]

Top
#66921 - 2002-06-14 01:20 AM Re: Ohms Law - Math routine
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Ah, I see another KiXtart Golf challenge coming up. Task: Write a UDF that creates an approximation of SIN() with better than 1% precision [Wink]
_________________________
There are two types of vessels, submarines and targets.

Top
#66922 - 2002-06-14 01:22 AM Re: Ohms Law - Math routine
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
jens please, no complex math anymore...
uttleast for some 6 months, please!
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.058 seconds in which 0.028 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org