First of all, I am new to F#, so sorry if this question already has answers here (I've tried looking around, but couldn't find what I was looking for). With this out of the way, let's go to my issue...
I am trying to access F# results from C#, but these are strange. Here's my F# code:
namespace SimpleFSharpCode
module SFSC =
let num1: int = 1
let num2: int = 2
let add (n1: int) (n2: int) = n1 + n2
let sum2: int = add num1 num2
let sum3 =
let num3: int = 1
let num4: int = 2
let add2 (n1: int) (n2: int) = n1 + n2
add2 num3 num4
let sum4 num5 =
let num6: int = 6
let add3 (n1: int) (n2: int) = n1 + n2
add3 num5 num6
printfn "num1 = %i" num1
printfn "num2 = %i" num2
printfn "sum2 = %i" sum2
printfn "sum3 = %i" sum3
printfn "sum4 = %i" (sum4 5)
It yields the results:
num1 = 1
num2 = 2
sum2 = 3
sum3 = 3
sum4 = 11
Here's my C# code:
using SimpleFSharpCode;
var num1 = SFSC.num1;
var num2 = SFSC.num2;
var sum1 = SFSC.add(num1, num2);
var sum2 = SFSC.sum2;
var sum3 = SFSC.sum3;
var sum4 = SFSC.sum4(5);
Console.WriteLine("num1 = " + num1);
Console.WriteLine("num2 = " + num2);
Console.WriteLine("sum1 = " + sum1);
Console.WriteLine("sum2 = " + sum2);
Console.WriteLine("sum3 = " + sum3);
Console.WriteLine("sum4 = " + sum4);
It yields the results:
num1 = 1
num2 = 2
sum1 = 3
sum2 = 0
sum3 = 0
sum4 = 11
As you can see, sum2 = sum3 = 0, which is not what I would expect. I would instead expect sum2 = sum3 = sum1 = 3, as is the case when I execute the F# script directly. What's strange is that sum4 = 11, which is the result I expect.
With this in mind, I wonder:
Why are sum2 = sum3 = 0 in my C# code but not in the F# code?
How should I write my code such that sum2 = sum3 = 3 when I call the C# script; i.e. how do I access the F# results from my C# script?
After some good comments from #Bent Tranberg and #Jim Foye to point me in the right direction, I managed to figure it out...
It turns out that the problem was in the .fsproj file. Basically, I had it setup as a console project (which was necessary in order to execute the F# script from F#), but it has to be a class project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- <OutputType>Exe</OutputType> --> // <-- THIS HAS TO BE COMMENTED OUT OR REPLACED
// WITH <OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
When the <OutputType>Exe</OutputType> is commented out, the C# script generates the following result:
num1 = 1
num2 = 2
sum2 = 3
sum3 = 3
sum4 = 11
num1 = 1
num2 = 2
sum1 = 3
sum2 = 3
sum3 = 3
sum4 = 11
Note that the printfn commands in the F# project are now also printed.
Related
At the moment i have this formula:
13^2 mod 5 = 4
I want to calculate back the 2 here like:
13^X mod 5 = 4
X = ?
I found several formula's/codes to do this online but i didn't find any which do it with a ^ symbol.
Would appreciate some help
My client received everything besides the TEST_PRIVATE so i want to calculate that.
This is the code i use (server sided)
(This is the encryption, not the decryption)
string TEST_GENERATED = "13";
string TEST_PRIVATE = "2";
string TEST_PRIME = "5";
BigIntegerTEST TESTMOD_1 = new BigIntegerTEST(TEST_GENERATED, 10);
BigIntegerTEST TESTMOD_2 = new BigIntegerTEST(TEST_PRIVATE, 10);
BigIntegerTEST TESTMOD_3 = new BigIntegerTEST(TEST_PRIME, 10);
BigIntegerTEST TESTMOD_4 = TESTMOD_1.modPow(TESTMOD_2, TESTMOD_3);
So basicly i want to reverse TESTMOD_4 to TESTMOD_2
By only using TESTMOD_4, TESTMOD_3 and TESTMOD_1
(I know modPow usually has 3 parameters i'm using a special class for it)
TDLR;
Working example:
(6 + 7) MOD 10 = 3
(3 - 6 + 10) MOD 10 = 7
This is the main result i want:
( I want to retrieve the 7)
(6^7) MOD 10 = 6
? = 7
it is here and much simple like this function:
public double DoCalc(double Number1, double Number2, double Number3)
{ return (Math.Pow(Number1, Number2)) % Number3; }
Then call it from the button click event, like this:
private void btmDoCalc_Click(object sender, EventArgs e)
{
// Here show it up in message box directly.
MessageBox.Show("DoCalc= " + DoCalc(13, 2, 5).ToString());
// Here assign it to some double variables.
double N1 = 0, N2 = 0, N3 = 0;
N1 = DoCalc(13, 2, 4);
N2 = DoCalc(13, 2, 3);
N3 = DoCalc(13, 2, 2);
MessageBox.Show("DoCalc= " + N1);
MessageBox.Show("DoCalc= " + N2);
MessageBox.Show("DoCalc= " + N3);
}
Here is some more images for the results.
I hope this answers your questions ^_^
Help, I just cannot get my head around this.
int value = 0;
int i = 5;
value -= i;
Console.WriteLine("Test5 = " + value.ToString());
value -= i - 2;
Console.WriteLine("Test5 = " + value.ToString());
Im thinking the output would be:
Test5 = -5 // value = 0 - 5
Test5 = -2 // value = -5 --5 -2
Why is the correct answer :
Test5 = -5
Test5 = -8
Where is the -8 coming from????
Ty
Simple mathematics.
int value = 0;
int i = 5;
value -= i;
Console.WriteLine("Test5 = " + value.ToString());
After the above code, we have the following values:
value = -5
i = 5
Next, you do this:
value -= i - 2;
Console.WriteLine("Test5 = " + value.ToString());
Which can be rewritten as (note the brackets around everything that came after the -=):
value = value - (i - 2);
i = 5, so i - 2 = 3.
-5 - 3 = -8.
The brackets, obviously, don't really exist. It's just that the right side of the -= (or +=, *=, /=, ^=, etc.) is evaluated to a single number first.
I don't know why but when I'm trying to compile the next code I'm getting error CS1525 and every ) at the end of every while command is being marked as an error:
static void PrintArray(string[] arr)
{
int i, sum = 0, subb = 0, pow, x;
char opper;
Console.WriteLine("how many numbers does your calculation have?");
i = Convert.ToInt16(Console.ReadLine());
arr = new string[i];
for (i = 0; i < arr.Length; i++)
{
Console.WriteLine("enter num {0}" + i);
arr[i] = Console.ReadLine();
Console.WriteLine("arr[{0}] = {1}" + i, arr[i]);
}
Console.WriteLine("what do you want to do?");
opper = Convert.ToChar(Console.ReadLine());
while (opper = +)
{
for (i = 0; i < arr.Length; i++)
{
sum = sum + Convert.ToInt16(arr[i]);
}
Console.WriteLine("your sum is " + sum);
}
while (opper = -)
{
for (i = 0; i < arr.Length; i++)
{
subb = subb + Convert.ToInt16(arr[i]);
}
Console.WriteLine("your subb is" + subb);
}
while (opper = *)
{
pow = Convert.ToInt16(arr[0]);
for (i = 1; i < arr.Length; i++)
{
pow = pow * Convert.ToInt16(arr[i]);
}
Console.WriteLine("the resolt is " + pow);
}
while (opper = &)
{
x = Convert.ToInt16(arr[i]);
for (i = 0; i < arr.Length; i++)
{
x = x / Convert.ToInt16(arr[i]);
}
Console.WriteLine("your resolt is " + x);
}
Console.ReadKey();
}
I will be glad if someone can finally explain that to me...
Given the lines (for example)
opper = Convert.ToChar(Console.ReadLine());
while (opper = +)
It looks like you're trying to compare the character input to an operator. You'll want to change the assignment operator to a comparison operator, and compare the character to another character, like so:
opper = Convert.ToChar(Console.ReadLine());
while (opper == '+')
user1673882 is correct here about the cause of the compile error. However, there are several other significant bugs you should be aware of as well.
As for the original compile issue, you have two issues with the following line (and all similar lines);
while (opper = +)
First, = (single "equals" sign) is assignment, not comparison. You want to use == here instead.
Secondly, + is not a character in this case, it's an operation. (In fact, the compiler can't infer exactly which operator it might be).
Even if you get this to compile, though, it won't work because all of your loops are infinite loops. Consider this example:
char myChar = 'a';
// Infinite loop
while (myChar == 'a')
{
Console.WriteLine("Test");
}
How could this possibly get out of the loop, given that myChar will always be a?
A few other miscellaneous bugs follow:
subb = subb + Convert.ToInt16(arr[i]);
This could be shortened with
subb += Convert.ToInt16(arr[i]);
or possibly even
subb += (short)arr[i];
Also, I'm assuming this shouldn't be "+" since that's exactly the same operation you're doing if the operation is "+" (i.e. the outcome of "+" and "-" should be exactly the same).
x = x / Convert.ToInt16(arr[i]);
First, same cleanup as above:
x /= (short)arr[i];
Secondly, you never test for division by 0 here, so this might throw an exception.
Third, I'm not sure what type x is, but "short" is definitely not closed over division - i.e.:
short a = ...
short b...
// May not be another short
Console.WriteLine(a / b);
Actually, this applies to multiplication, subtraction, and addition to some extent too in this case since shorts have a finite size. Consider the following code:
short overflow = short.MaxValue;
// -32768
overflow++;
// +32767
overflow--;
// -32768 again
overflow++;
// -32767
overflow++;
checked
{
overflow = short.MaxValue;
// Now this results in an OverflowException
overflow++;
}
One more example:
short testArithmetic = 1;
// This gives us the result that 1 / 2 = 0.
testArithmetic /= 2;
// Set this back to 1 for the next operation
testArithmetic = 1;
// This is 0.0 too!
double testArithmeticFloat = testArithmetic / 2;
// This gives us the result we'd expect
testArithmeticFloat = 1.0 / 2.0;
// This'll compile just fine, but you get a DivideByZeroException when you try to execute it
testArithmetic /= 0;
This question already has answers here:
Is (--i == i++) an Undefined Behavior?
(8 answers)
Closed 8 years ago.
I know the below code will lead to undefined behaviour according to c/c++ standard but what about in c#? ,After some searching I found that in c# all the arguments/variables in an expression are evaluated from left to right(please correct me if am wrong), If this is true than the result(output of res variable) of below program should be 3, but its 4 ??.
class Program
{
static void Main(string[] args)
{
int a = 1;
int res = (a++) + (++a); //Will This Lead to Undefined Behavior(Like in C/C++)..??
Console.WriteLine(res);
Console.ReadLine();
}
}
The result is fine for these expressions when checked with left to right evaluation.
res = a + ++a; \\Successfully evaluates res to 3
res = ++a + a; \\Sussessfully evaluates res to 4
res = ++a + a++; \\Successfully evaluates res to 4
Similarly
res= a++ + ++a ;\\should be 3, why i get it 4 ??
Can Anybody explain am confused.??
Your program is equivalent to:
var a = 1;
var temp0 = a++;
var temp1 = ++a;
var res = temp0 + temp1;
And the result of that is 4.
Even simpler:
var a = 1;
var temp0 = a; a++;
a++; var temp1 = a;
var res = temp0 + temp1;
C# does not have Undefined Behavior like C/C++. That would undermine the security properties of .NET. That said .NET can and does have implementation defined behavior in a few places. Not here, though.
a++ == 1 (now, a = 2), then ++a == 3
1 + 3 = 4
Seems right to me.
The difference between var b = a++ and var b = ++a is the time that the variable a gets changed.
b = a++ first assigns the value of a to variable b and then increases a.
b = ++a first increases the value of a and then assigns it to variable b.
The time of the assignment makes the difference. So here is your code annotated:
int a = 1; // a = 1
int res =
a++ // the value of a (1 is inserted into the equation) and a is increased by 1 - so a == 2
+ ++a // a is increased by one (a == 3) and its value is inserted into the equation
; // (res = (1 + 3)) == 4
To make it clearer, the code could be rewritten as follows. And actually this is how the C# stack evaluates your code:
int a = 1;
int res = 0;
res += a; // add a to res - a == 1, res = 1
a += 1; // increase a (could also be written as a++ or ++a); - a == 2, res = 1
a += 1; // increase a again (could also be written as a++ or ++a); - a == 3, res = 1
res += a; // add a to res - a == 3, res = 4
In the equation a's value is used and not it's reference. So each time the variable a occurs in the equation its current value gets pushed to the stack and is then used in the subsequent evaluation of the equation.
start condition a=1, res=0
res = a++ => res = 1, a = 2
+ ++a => a=3 BEFORE addition, res = 4, result is correct
I agree with Volte and davidc. Just try use two variables instead of one (a). For example:
int a = 1;
int b = 1;
Console.WriteLine("{0}", a++ + ++b); //this write 3 on console
Console.ReadLine();
This proves that our threory is true.
So I'm building a small math game where random sums are generated and, upon being answered, added to either a right or wrong score. It's going well, and I've received some help with certain things and learnt along the way, but I've hit another problem I can't figure out. The game picks between +, -, * and / operators when generating sums, and +, - and * work well, but / often calls for a decimal answer which the program does not like. I'd like to try to figure out a way to make it not generate a number to divide by that would result in a decimal answer when diving the first number. Here's some example code to clear what I have so far up:
var randomNum = new Random();
num1 = randomNum.Next(0, 10);
num2 = randomNum.Next(0, 10);
char[] operators = { '+', '-', '*', '/' };
char op = operators[randomNum.Next(operators.Length)];
switch (op)
{
case '+':
answer = num1 + num2;
label1.Text = num1.ToString() + " + " + num2.ToString() + " = ";
break;
case '-':
answer = num1 - num2;
label1.Text = num1.ToString() + " - " + num2.ToString() + " = ";
break;
case '*':
answer = num1 * num2;
label1.Text = num1.ToString() + " * " + num2.ToString() + " = ";
break;
case '/':
answer = num1 / num2;
label1.Text = num1.ToString() + " / " + num2.ToString() + " = ";
break;
}
I've tried moving the bits that state what num1 and num2 are into each of the cases, so that they look like this:
case '/':
num1 = randomNum.Next(0, 10);
num2 = randomNum.Next(0, 10);
answer = num1 / num2;
label1.Text = num1.ToString() + " / " + num2.ToString() + " = ";
break;
But I can't conceive of what I could put in the brackets instead of having (0, 10) to avoid decimal sum answers. Is there a way I can have it determine if an answer will be a decimal one, and if it is re-roll num2 to try and get a whole number answer? Thanks!
Because integer division rounds down, you should change num1 to ensure an exact division:
num1 = randomNum.Next(0, 10);
num2 = randomNum.Next(1, 10);//Cannot divide by 0!!
answer = num1 / num2;
num1 = answer * num2;
if num1 = 7 and num2 = 3, answer will be 2 and num1 is changed to 6; making num1 == 6, num2 == 3 and answer == 2
You could also generate num2 and answer and calculate num1
num2 = randomNum.Next(1, 10);//Cannot divide by 0!!
answer = randomNum.Next(0, 10);
num1 = answer * num2; // answer == num1 / num2
Pretty simple. Send in the whole number, you'll get out a number that will
divide evenly (easy for a kid).
public static class TestWholeNumber
{
public static int ReturnWholeNumber(int testnum)
{
var randomNum = new Random(Guid.NewGuid().GetHashCode());
int num2 = 0;
do
{
num2 = randomNum.Next(1, 10);
}
while (testnum % num2 != 0);
return num2;
}
}