#66903 - 2002-06-13 10:56 AM
Ohms Law - Math routine
|
NTDOC
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...
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
|
|
|
|
#66905 - 2002-06-13 11:46 AM
Re: Ohms Law - Math routine
|
Richard H.
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
|
|
|
|
#66908 - 2002-06-14 12:40 AM
Re: Ohms Law - Math routine
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
sorry richard, but now is time to say to you: RTFM!
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
   
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]](images/icons/wink.gif) [ 13 June 2002, 12:58: Message edited by: Richard Howarth ]
|
|
Top
|
|
|
|
#66911 - 2002-06-13 01:50 PM
Re: Ohms Law - Math routine
|
Richard H.
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
|
|
|
|
#66913 - 2002-06-13 02:07 PM
Re: Ohms Law - Math routine
|
BrianTX
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
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
|
|
Top
|
|
|
|
#66915 - 2002-06-13 05:45 PM
Re: Ohms Law - Math routine
|
Kdyer
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
|
|
Top
|
|
|
|
#66916 - 2002-06-13 06:49 PM
Re: Ohms Law - Math routine
|
NTDOC
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.
|
|
Top
|
|
|
|
#66917 - 2002-06-13 07:07 PM
Re: Ohms Law - Math routine
|
Kdyer
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."
??
Kent
|
|
Top
|
|
|
|
#66918 - 2002-06-13 07:12 PM
Re: Ohms Law - Math routine
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
never forget the Heisenberg Uncertainty Principle http://www.aip.org/history/heisenberg/
|
|
Top
|
|
|
|
#66919 - 2002-06-13 09:21 PM
Re: Ohms Law - Math routine
|
Kdyer
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!!
Kent
|
|
Top
|
|
|
|
#66920 - 2002-06-14 12:54 AM
Re: Ohms Law - Math routine
|
NTDOC
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?...
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1636 anonymous users online.
|
|
|