Why does this return 0? [duplicate] - c#

This question already has answers here:
No overflow exception for int in C#?
(6 answers)
Closed 4 years ago.
using System;
namespace test_warmup
{
class Program
{
static void Main(string[] args)
{
int test = -1110835200;
test = test * 1700397056;
Console.WriteLine(test);
}
}
}

-1110835200 * 1700397056 = -1888860903781171200
Displayed in hex, that's -0x1a3694fc_00000000
In C#, int is only 32-bits, so the result is truncated to just 0.
In other words, the result of that multiplication is too large to fit in the variable to which you're assigning it, and the part that fits is all zero.

Related

Getting the last numbers from a string [duplicate]

This question already has answers here:
Select last element quickly after a .Split()
(6 answers)
Extract version number from string(eg : "ver.1.9.0")
(3 answers)
Closed 2 years ago.
Hello I need to get last number from 1.0.0.0 but the number will change so eventually it can be 1.0.0.111
so I have stripped the number from "."
var amount = "1.0.0.23";
var pureAmount = amount.Replace(#".", "");
Console.WriteLine(pureAmount);
and then I have this extension method that returns the number
public static class StringExtension
{
public static string GetLast(this string source, int tail_length)
{
if(tail_length >= source.Length)
return source;
return source.Substring(source.Length - tail_length);
}
}
used like this
Console.WriteLine(amount.GetLast(1));
But what if I don't know how many digits will the last number have? I just need the number behind the last "." is there a way to do the?
You can achieve this using single statement. Try string functions Split and Last
var result = amount.Split('.').Last();

Array returns System.Int32[]? [duplicate]

This question already has answers here:
System.Int32[] displaying instead of Array elements [duplicate]
(4 answers)
Closed 2 years ago.
When I make a function to return an array with the correct results.
Instead of giving me the correct results, I get as result System.Int32[].
Anyone an idea why this is?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MultiplyByLength(new int[] {2,3,1,0}));
}
public static int[] MultiplyByLength(int[] arr)
{
return arr.Select(x => x * arr.Length).ToArray();
}
}
You need to format it some how. An array doesn't have a ToString() override that knows how you want to format your type (int[]) to a string, in such cases it just returns the type name (which is what you are seeing)
foreach(var item in MultiplyByLength(new int[] {2,3,1,0})
Console.WriteLine(item);
or
Console.WriteLine(string.Join(Environment.NewLine, MultiplyByLength(new int[] {2,3,1,0}));
or
Console.WriteLine(string.Join(",", MultiplyByLength(new int[] {2,3,1,0}));

Is it possible to test an entire array at once? [duplicate]

This question already has answers here:
Easiest way to compare arrays in C#
(19 answers)
Closed 3 years ago.
Is there a way to use an if statement to test an entire array at once by doing something like this:
if(myArray == {1,2,3})
{Debug.Log("This is quick")}
or do I need to iterate through each value in the array like this:
if(myArray[0] == 1 && myArray[1] == 2 && myArray[2] == 3)
{Debug.Log("This is not as quick")}
You should use SequenceEqual.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int [] myArray = {1,2,3};
int [] myArray2 = {1,2,4};
bool result = myArray.SequenceEqual(myArray2);
}
}

Working with Decimals and Integers [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 5 years ago.
I'm sure this is a stupid question but I'm a newbie!
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var myInt = ((1000/2048) * 1536);
Console.WriteLine(myInt);
}
}
}
Output is:
0
Can someone tell me how to get the correct number (750)?
By default, 1000 and 2048 are int, therefore the result will be treated as an int.
You have to cast them to double.
Option 1:
var myInt = (((double)1000 / 2048) * 1536);
Console.WriteLine(myInt);
Option 2:
var myInt = ((1000.0 / 2048) * 1536);
Console.WriteLine(myInt);
You should read about value types in C#:
https://www.tutorialspoint.com/csharp/csharp_data_types.htm
https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx

The type or namespace name 'Numerics' does not exist in the namespace 'System' [duplicate]

This question already has answers here:
Can't get System.Numerics to work with command-line Mono (mcs) on OS X
(1 answer)
Where is my System.Numerics namespace?
(2 answers)
Closed 9 years ago.
I am solving problem 25 on Project Euler. I wrote a code for it in C#. However in this case I needed to use "BigInt", since Int64 wasn't big enough to hold the number. But, when I put using System.Numerics;, it gives an error message during compiling (the message in title). Why is this? I am using Mono 2.10.9.
My code:
using System;
using System.Numerics;
public class Problem_25{
static BigInt fib(int n){
double Phi = (1+Math.Sqrt(5))/2;
double phi = (1-Math.Sqrt(5))/2;
BigInt an = Convert.BigInt((Math.Pow(Phi, n)-(Math.Pow(phi, n)))/Math.Sqrt(5));
return an;
}
static void Main(){
int i = 100;
int answer = 0;
string current_fn = "1";
while(true){
current_fn = Convert.ToString(fib(i));
if(current_fn.Length == 1000){
answer = i;
break;
}
else{
i++;
}
}
Console.WriteLine("Answer: {0}", answer);
}
}
You need to add a reference to System.Numerics.dll.
mcs -r:System.Numerics.dll main.cs
man mcs should do you the honours.
Depending on your mono version you might want to use dmcs and/or upgrade.
Oh, and it's BigInteger, not BigInt

Categories

Resources