Jump to content

Error with number output in C#?

AlTech

So yeah,

 

I'm having an issue with a numerical output coming out really weirdly.

 

Does anybody know why this is happening?

 

58d7b20e4f9ae_benchmarkerror.PNG.a9b6a7f49ca497ee6f897cd40311a74b.PNG

 

Here's the code from my mathematical calculations for working out the millions of calculations per second:

 

Dividing multiTime by 1000 is to turn miliseconds into seconds.

 

the 750 Million + 750 Million is because 2 types of calculations are done and each does 750 Million calculations.

 

The multi threaded does 750 Million calcs in one thread and the other 750 Million in a second thread.

       public string multiThreadedScorePerSecond()
        {
            //Convert to calcs per second
            multiTimePerSec = (750000000 + 750000000) / (multiTime / 1000);

            //Convert to millions of calcs per second
            multiTimePerSec = multiTimePerSec / 1000;
            multiTimePerSec = multiTimePerSec / 1000;

            multiTimePerSec = multiTimePerSec / multiTime;

            return multiTimePerSec.ToString() + " Millions of calculations per second.";
        }

 

The single threaded does the first type of calc and then the second type of calc sequentially.

        public string singleThreadedScorePerSecond()
        {
            //Convert to calcs per second
            singleTimePerSec = (750000000 + 750000000) / (singleTimePerSec / 1000);

            //Convert to millions of calcs per second
            singleTimePerSec = singleTimePerSec / 1000;
            singleTimePerSec = singleTimePerSec / 1000;

            singleTimePerSec = singleTimePerSec / singleTime;

            return singleTimePerSec.ToString() + " Millions of calculations per second.";
        }

Thanks.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Can you post the full code? Also, why are you diving multiTimePerSec & singleTimePerSec by 1000, twice?

 

multiTimePerSec = multiTimePerSec / 1000;
multiTimePerSec = multiTimePerSec / 1000;
singleTimePerSec = singleTimePerSec / 1000;
singleTimePerSec = singleTimePerSec / 1000;
Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, alca said:

Can you post the full code? Also, why are you diving multiTimePerSec & singleTimePerSec by 1000, twice?

 


multiTimePerSec = multiTimePerSec / 1000;
multiTimePerSec = multiTimePerSec / 1000;

singleTimePerSec = singleTimePerSec / 1000;
singleTimePerSec = singleTimePerSec / 1000;

I want to turn the 1.5 Billion calcs / second to turn it into Millions per second and so I figured I could do it this way......

 

The full code is too much for here so i'll save you the trouble and give you a link to it -> https://github.com/AluminiumTech/CSMark/tree/master/CSMark

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

1.5 bil / 1000 / 1000 ~ 15k calcs/s

1.5 bil / 1000 ~ 1.5 mil calcs/s

 

Even then I don't think this would cause the output thingy. 

Try looking at the singleTime variable and if it's maybe causing the issue. Other than that, I'm not sure and you should try posting the question @ http://www.stackoverflow.com

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, alca said:

1.5 bil / 1000 / 1000 ~ 15k calcs/s

1.5 bil / 1000 ~ 1.5 mil calcs/s

 

Even then I don't think this would cause the output thingy. 

Try looking at the singleTime variable and if it's maybe causing the issue. Other than that, I'm not sure and you should try posting the question @ http://www.stackoverflow.com

So I did some testing and it turned out that my Single Threaded number was being divided too many times.

 

Do you know how I could take my existing single threaded benchmark and make it multi threaded? Cos my existing multi threaded benchmark sucks.

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

improved.PNG.c0d9a71b5038db4b89c567e3edbcb1be.PNG

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, M.Yurizaki said:

It's possible you're not handling the data properly in a multithreaded environment, and so "multiTimePerSec" could mean anything by the time you're poking it.

 

I would have a better idea if more of the source code was available.

All the source code is available at the link. I'm not posting it here because it would be obnoxious to read the entirety of it.

 

https://github.com/AluminiumTech/CSMark/tree/master/CSMark

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, AluminiumTech said:

All the source code is available at the link. I'm not posting it here because it would be obnoxious to read the entirety of it.

 

https://github.com/AluminiumTech/CSMark/tree/master/CSMark

I don't think this code will do what you want it to do

multiTimeStopwatch.Start();

//Start the threads and make the benchmarking happen!
pyThread.Start();
trigThread.Start();

multiTimeStopwatch.Stop();

That's just timing how long it takes to start up the threads. It doesn't take into account how long the threads run for. You probably want something more like these answers describe - StackOverflow: How to wait for thread to finish with .NET?

 

edit (to answer your original question): So regarding the printing issue, I think it's because multiTimeStopwatch.ElapsedMilliseconds is zero because there's not enough precision to measure something that fast. Which means you're dividing by zero later. Here's some code to explain.

class Program
{
    static void Main(string[] args)
    {
        double multiTime = 0;
        double x = 1 / multiTime;
        Console.WriteLine(x); // prints weird character
      
        // If you were using integers it would throw an exception.
        // However double has the notion of Infinity and NaN so it doesn't throw an exception
        bool b = double.IsInfinity(x); // is true
        Console.WriteLine(b);
    }
}

Side note, some programs may properly print out an infinity character. Like LINQPad did when I tested with that.

ss2017-03-28%2009_29_38.jpg?raw=1

 

Another side note, if ElapsedMilliseconds isn't enough in the future you may be able to get a value from ElapsedTicks - StackOverflow: How do you convert Stopwatch ticks to nanoseconds, milliseconds and seconds? (I've never looked into the accuracy of this though)

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, madknight3 said:

I don't think this code will do what you want it to do


multiTimeStopwatch.Start();

//Start the threads and make the benchmarking happen!
pyThread.Start();
trigThread.Start();

multiTimeStopwatch.Stop();

That's just timing how long it takes to start up the threads. It doesn't take into account how long the threads run for. You probably want something more like these answers describe - StackOverflow: How to wait for thread to finish with .NET?

 

edit (to answer your original question): So regarding the printing issue, I think it's because multiTimeStopwatch.ElapsedMilliseconds is zero because there's not enough precision to measure something that fast. Which means you're dividing by zero later. Here's some code to explain.


class Program
{
    static void Main(string[] args)
    {
        double multiTime = 0;
        double x = 1 / multiTime;
        Console.WriteLine(x); // prints weird character
      
        // If you were using integers it would throw an exception.
        // However double has the notion of Infinity and NaN so it doesn't throw an exception
        bool b = double.IsInfinity(x); // is true
        Console.WriteLine(b);
    }
}

 

Thanks. I eventually managed to fix the single threaded test. The multi threaded test has yet to be fixed.

 

I'll take a look at the Stack Overflow article but I have a feeling I'll need to implement a while loop.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×