• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 12:05
CET 17:05
KST 01:05
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy7ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289
Community News
Weekly Cups (March 16-22): herO doubles, Cure surprises3Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool43Weekly Cups (March 9-15): herO, Clem, ByuN win42026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12
StarCraft 2
General
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Weekly Cups (March 16-22): herO doubles, Cure surprises Weekly Cups (August 25-31): Clem's Last Straw? Team Liquid Map Contest #22 - Presented by Monster Energy What mix of new & old maps do you want in the next ladder pool? (SC2)
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament World University TeamLeague (500$+) | Signups Open RSL Season 4 announced for March-April WardiTV Team League Season 10 KSL Week 87
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026]
External Content
The PondCast: SC2 News & Results Mutation # 518 Radiation Zone Mutation # 517 Distant Threat Mutation # 516 Specter of Death
Brood War
General
mca64Launcher - New Version with StarCraft: Remast BGH Auto Balance -> http://bghmmr.eu/ ASL21 General Discussion Soulkey's decision to leave C9 JaeDong's form before ASL
Tourneys
[ASL21] Ro24 Group B 2026 Changsha Offline Cup [ASL21] Ro24 Group A ASL Season 21 LIVESTREAM with English Commentary
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2
Other Games
General Games
General RTS Discussion Thread Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread European Politico-economics QA Mega-thread YouTube Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books Movie Discussion! [Manga] One Piece
Sports
2024 - 2026 Football Thread Cricket [SPORT] Formula 1 Discussion Tokyo Olympics 2021 Thread General nutrition recommendations
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 3298 users

The Big Programming Thread - Page 243

Forum Index > General Forum
Post a Reply
Prev 1 241 242 243 244 245 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
Last Edited: 2013-02-13 19:07:25
February 13 2013 19:05 GMT
#4841
that's sexy americanumlaut!

i was thinking bitwise shift operator and modulo, but it's probably not a portable solution, yours seems solid though.
it works for any number in the digit, decimal or not as well.
conspired against by a confederacy of dunces.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-13 19:33:28
February 13 2013 19:29 GMT
#4842
On February 14 2013 02:56 supereddie wrote:
Show nested quote +
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator


Unfortunately, I'm not sure if I understand his code.

Is all of this code needed to be executed?

int(value * pow(10, decimalnumber)) % 10 <-----
2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

If yes, what is int here? Maybe (int)?

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.

WindWolf
Profile Blog Joined July 2012
Sweden11767 Posts
February 13 2013 19:32 GMT
#4843
Just a general advice

Multiple inheritance in programming. Avoid it as far as it is possible. While it may sound cool, it may also be very very confusing for someone who should work with your code and hasn't followed the development of it.
EZ4ENCE
CountChocula
Profile Blog Joined January 2011
Canada2068 Posts
Last Edited: 2013-02-13 20:11:54
February 13 2013 19:36 GMT
#4844
On February 14 2013 04:29 darkness wrote:
Show nested quote +
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.


the double has a lot higher precision than gets displayed. printf will display 6 digits after the decimal point by default. if you want to display more decimal digits, you have to specify i.e. printf("%.9f", value) will give you 9 digits after the decimal point.

int(value * pow(10, n)) % 10


you just need the one line that AmericanUmlaut provided. it does 3 things in this order:

1. multiply value by 10^n where n refers to the n'th digit you want (look up what pow function does if you don't know)
2. cast to int - he uses a different notation than the one you use. (int) x and int(x) mean the same thing
3. do % 10 which in math means "mod 10"

1. 2.283214570 * 100 => 228.3214570
2. cast to int => 228
3. 228 mod 10 => 8
Writer我会让他们连馒头都吃不到 Those championships owed me over the years, I will take them back one by one.
Snuggles
Profile Blog Joined May 2010
United States1865 Posts
February 13 2013 19:58 GMT
#4845
On February 13 2013 22:56 waxypants wrote:
Show nested quote +
On February 13 2013 02:40 Snuggles wrote:
No it wants me to write out the Class, not use it. At some point I remember getting some feedback showing the input, how it runs through my methods.

Here's the problem before that one that I managed to get right.

Problem
+ Show Spoiler +
Write the definition of a class Counter containing:
An instance variable named counter of type int .
A constructor that takes one int argument and assigns its value to counter
A method named increment that adds one to counter . It does not take parameters or return a value.
A method named decrement that subtracts one from counter . It also does not take parameters or return a value.
A method named get_value that returns the value of the instance variable counter .


Answer
+ Show Spoiler +
class Counter:
def __init__(self):
self.counter = 0
def increment(self):
self.counter += 1
def decrement(self):
self.counter -= 1
def get_value (self):
return self.counter


They took that one in real easily. I really don't know where the hell I messed up in the Player problem...


Actually the __init__ should be something like:


def __init__(self, counter):
self.counter = counter


If I did that would I have to give the counter a default value? I feel like self.counter = counter wouldn't work unless the user inputted a value for counter.


def__init__(self,counter = 0):
self.counter = counter


Like say what would happen if someone did xxx = Counter()?
ragnorr
Profile Joined April 2011
Denmark6097 Posts
Last Edited: 2013-02-13 20:07:06
February 13 2013 20:06 GMT
#4846
On February 14 2013 04:58 Snuggles wrote:
Show nested quote +
On February 13 2013 22:56 waxypants wrote:
On February 13 2013 02:40 Snuggles wrote:
No it wants me to write out the Class, not use it. At some point I remember getting some feedback showing the input, how it runs through my methods.

Here's the problem before that one that I managed to get right.

Problem
+ Show Spoiler +
Write the definition of a class Counter containing:
An instance variable named counter of type int .
A constructor that takes one int argument and assigns its value to counter
A method named increment that adds one to counter . It does not take parameters or return a value.
A method named decrement that subtracts one from counter . It also does not take parameters or return a value.
A method named get_value that returns the value of the instance variable counter .


Answer
+ Show Spoiler +
class Counter:
def __init__(self):
self.counter = 0
def increment(self):
self.counter += 1
def decrement(self):
self.counter -= 1
def get_value (self):
return self.counter


They took that one in real easily. I really don't know where the hell I messed up in the Player problem...


Actually the __init__ should be something like:


def __init__(self, counter):
self.counter = counter


If I did that would I have to give the counter a default value? I feel like self.counter = counter wouldn't work unless the user inputted a value for counter.


def__init__(self,counter = 0):
self.counter = counter


Like say what would happen if someone did xxx = Counter()?

You are forced to give a value along when you create the object tho since the constructor takes the value as a argument?
supereddie
Profile Joined March 2011
Netherlands151 Posts
Last Edited: 2013-02-13 20:18:58
February 13 2013 20:15 GMT
#4847
On February 14 2013 04:29 darkness wrote:
Show nested quote +
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator


Unfortunately, I'm not sure if I understand his code.

Is all of this code needed to be executed?

int(value * pow(10, decimalnumber)) % 10 <-----
2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

If yes, what is int here? Maybe (int)?

It mostly depends on the language, but it generally means that you will be casting the result of 'value * pow(10, decimalnumber)' to an integer. Casting to a 'lower' datatype means you're losing data, but also that you might get overflows when the result of (value * pow(10, decimalnumber)) > Int32.MaxValue. Therefore, it may be more prudent to convert the result of the entire operation (including the mod 10) to an integer. There are still changes of an overflow for the double for very large values, but I think those are negligible. Also, casting to an integer means losing precision; it will not round the number in any way; 1.9 becomes 1 (and not 2).

I suggest converting to an integer (or byte, as the number can only be between 0 - 9) as late as possible to avoid overflows.
For very robust programming you can remove unneeded digits as they are not relevant for your particular problem. With that I mean the for the number 12345.678, you can actually remove everything before the decimal point so the number you work with becomes 0.678 (something like value = value - Math.Floor(value)). That should solve the overflow problems.
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-13 21:52:42
February 13 2013 21:34 GMT
#4848
On February 14 2013 04:36 CountChocula wrote:
Show nested quote +
On February 14 2013 04:29 darkness wrote:
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.


the double has a lot higher precision than gets displayed. printf will display 6 digits after the decimal point by default. if you want to display more decimal digits, you have to specify i.e. printf("%.9f", value) will give you 9 digits after the decimal point.

int(value * pow(10, n)) % 10


you just need the one line that AmericanUmlaut provided. it does 3 things in this order:

1. multiply value by 10^n where n refers to the n'th digit you want (look up what pow function does if you don't know)
2. cast to int - he uses a different notation than the one you use. (int) x and int(x) mean the same thing
3. do % 10 which in math means "mod 10"

1. 2.283214570 * 100 => 228.3214570
2. cast to int => 228
3. 228 mod 10 => 8


Thanks. However, there may be a bug. It says -8 for decimalnumber >= 10. I guess there aren't enough decimal places?

(int)(value * pow(10, decimalnumber)) % 10

Edit: C language

Edit 2: My lecturer replied with:

The precision requirement is very high, so built-in variables are not
good enough.


Could you give me a hint what to look for? GMP?
enigmaticcam
Profile Blog Joined October 2010
United States280 Posts
February 13 2013 21:50 GMT
#4849
On February 13 2013 23:19 alwinuz wrote:If you want to use a proxy server, you can assign the default proxy to your web request or something simililar. Maybe it is also possible to set the proxy for your code that makes the FTP connection.

We had a similar problem where we could not connect to a old asmx webservice because we didn't use the system proxy.
Now we have this code in the client to connect to the webservice (ws variable):
// Make sure that webservice calls can pass through intranet proxy
ws.Credentials = CredentialCache.DefaultCredentials;
ws.Proxy = WebRequest.DefaultWebProxy;
if (ws.Proxy != null) ws.Proxy.Credentials = CredentialCache.DefaultCredentials;


I hope this helps, not sure if I understood your question correctly.
Else you can maybe try to start up ping.exe with Process.Start() and parse the output?

This got me in the right direction. I found that I can setup a proxy on an ftpwebrequest. I'll have to play around with it, but I'm sure I can get it to work.

Parsing the output in ping won't work because on our network you can't ping any internet hostname. Can't ping google.com, cnn.com, or the like. Asinine, isn't it!

Thanks
CountChocula
Profile Blog Joined January 2011
Canada2068 Posts
Last Edited: 2013-02-13 22:41:13
February 13 2013 22:03 GMT
#4850
On February 14 2013 06:34 darkness wrote:
Show nested quote +
On February 14 2013 04:36 CountChocula wrote:
On February 14 2013 04:29 darkness wrote:
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.


the double has a lot higher precision than gets displayed. printf will display 6 digits after the decimal point by default. if you want to display more decimal digits, you have to specify i.e. printf("%.9f", value) will give you 9 digits after the decimal point.

int(value * pow(10, n)) % 10


you just need the one line that AmericanUmlaut provided. it does 3 things in this order:

1. multiply value by 10^n where n refers to the n'th digit you want (look up what pow function does if you don't know)
2. cast to int - he uses a different notation than the one you use. (int) x and int(x) mean the same thing
3. do % 10 which in math means "mod 10"

1. 2.283214570 * 100 => 228.3214570
2. cast to int => 228
3. 228 mod 10 => 8


Thanks. However, there may be a bug. It says -8 for decimalnumber >= 10. I guess there aren't enough decimal places?

(int)(value * pow(10, decimalnumber)) % 10

Edit: C language

it's a sign that int is overflowing, which makes sense because int's range is from -2147483648 to 2147483647. if you try to assign 2147483647 + 1 to an int, it will loop back to -2147483648.

since you're dealing with overflows you should use supereddie's implementation, because it protects you from overflow problems.



edit: btw what's the biggest decimalnumber you need to use?

the precision of a double is about 15 digits, so if you need decimalnumber = 15 or >15 you might need to do something else (GMP or your own implementation of a division algorithm). i'm not sure but this might be what your teacher is hinting at. in any case, you should try supereddie's solution first to see if that solves your problems.
Writer我会让他们连馒头都吃不到 Those championships owed me over the years, I will take them back one by one.
3FFA
Profile Blog Joined February 2010
United States3931 Posts
February 13 2013 23:43 GMT
#4851
I have a question(not homework, just curious)

Is there a way that, if say someone put in David Jacob Young

that I could use strings in C to find the initials and use a printf to print them out to the user?

David Jacob Young
DJY
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-14 00:28:47
February 13 2013 23:51 GMT
#4852
On February 14 2013 07:03 CountChocula wrote:
Show nested quote +
On February 14 2013 06:34 darkness wrote:
On February 14 2013 04:36 CountChocula wrote:
On February 14 2013 04:29 darkness wrote:
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.


the double has a lot higher precision than gets displayed. printf will display 6 digits after the decimal point by default. if you want to display more decimal digits, you have to specify i.e. printf("%.9f", value) will give you 9 digits after the decimal point.

int(value * pow(10, n)) % 10


you just need the one line that AmericanUmlaut provided. it does 3 things in this order:

1. multiply value by 10^n where n refers to the n'th digit you want (look up what pow function does if you don't know)
2. cast to int - he uses a different notation than the one you use. (int) x and int(x) mean the same thing
3. do % 10 which in math means "mod 10"

1. 2.283214570 * 100 => 228.3214570
2. cast to int => 228
3. 228 mod 10 => 8


Thanks. However, there may be a bug. It says -8 for decimalnumber >= 10. I guess there aren't enough decimal places?

(int)(value * pow(10, decimalnumber)) % 10

Edit: C language

it's a sign that int is overflowing, which makes sense because int's range is from -2147483648 to 2147483647. if you try to assign 2147483647 + 1 to an int, it will loop back to -2147483648.

since you're dealing with overflows you should use supereddie's implementation, because it protects you from overflow problems.



edit: btw what's the biggest decimalnumber you need to use?

the precision of a double is about 15 digits, so if you need decimalnumber = 15 or >15 you might need to do something else (GMP or your own implementation of a division algorithm). i'm not sure but this might be what your teacher is hinting at. in any case, you should try supereddie's solution first to see if that solves your problems.



I wasn't told how big, but the sample double is: 0.615384615384615384615384... so yeah, it looks huge. No idea how to implement this (int)(value * pow(10, decimalnumber)) % 10 via GMP for higher precision.


On February 14 2013 08:43 3FFA wrote:
I have a question(not homework, just curious)

Is there a way that, if say someone put in David Jacob Young

that I could use strings in C to find the initials and use a printf to print them out to the user?

David Jacob Young
DJY


You may want to read this
http://www.dreamincode.net/forums/topic/242738-full-names-to-initials-keeping-surname/
delHospital
Profile Blog Joined December 2010
Poland261 Posts
February 14 2013 00:38 GMT
#4853
On February 14 2013 06:34 darkness wrote:
Show nested quote +
On February 14 2013 04:36 CountChocula wrote:
On February 14 2013 04:29 darkness wrote:
On February 14 2013 02:56 supereddie wrote:
On February 13 2013 13:10 AmericanUmlaut wrote:
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

Doh, ofcourse! How could I forget about the mod operator

Also, I am wondering now why my double 'value' sas only 6 decimal digits. This is what I do:

int a, b;
double value;
scanf("%d %d", &a, &b);
value = (double)a / (double) b; <---- only 6 decimal digits (e.g. 8/13)

Even if I change 'int' to 'float, it is still the same.


the double has a lot higher precision than gets displayed. printf will display 6 digits after the decimal point by default. if you want to display more decimal digits, you have to specify i.e. printf("%.9f", value) will give you 9 digits after the decimal point.

int(value * pow(10, n)) % 10


you just need the one line that AmericanUmlaut provided. it does 3 things in this order:

1. multiply value by 10^n where n refers to the n'th digit you want (look up what pow function does if you don't know)
2. cast to int - he uses a different notation than the one you use. (int) x and int(x) mean the same thing
3. do % 10 which in math means "mod 10"

1. 2.283214570 * 100 => 228.3214570
2. cast to int => 228
3. 228 mod 10 => 8


Thanks. However, there may be a bug. It says -8 for decimalnumber >= 10. I guess there aren't enough decimal places?

(int)(value * pow(10, decimalnumber)) % 10

Edit: C language

Edit 2: My lecturer replied with:

Show nested quote +
The precision requirement is very high, so built-in variables are not
good enough.


Could you give me a hint what to look for? GMP?

I'd just write my own division function that works like long division and stops after finding the decimalnumber-th digit.
3FFA
Profile Blog Joined February 2010
United States3931 Posts
February 14 2013 00:47 GMT
#4854
@darkness
Having only programmed in C, and this being my first time learning, I didn't understand most of that. Was that perhaps C++? (Has never seen strings declared that way before, with '<<')
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
phar
Profile Joined August 2011
United States1080 Posts
Last Edited: 2013-02-14 04:08:36
February 14 2013 04:03 GMT
#4855
On February 13 2013 13:10 AmericanUmlaut wrote:
Show nested quote +
On February 13 2013 07:43 darkness wrote:
On February 13 2013 05:32 supereddie wrote:
On February 13 2013 04:18 darkness wrote:
Hey guys, thanks for helping me last time. I have another thing that I struggle with.

Say you have a double variable with this value:

double value = a / b;


let's say 'value = 2.283214570' after division

'a' and 'b' are both integers.

Now, my problem is that I need to print the n-th digit of this double variable. E.g. n = 2

Then output would be '8' because it's the 2nd digit after decimal point. I've thought about double -> string, but my implementation doesn't work for integers that are up to 60000 which is a requirement.


Just use math?
Multiply 'value' with 10^decimalnumber (so you have 228.3214570). Take the int part of it (=228).
Do it again with decimalnumber - 1 and take int part: 22. Multiply by 10 = 220.
Then just substract the two: 228 - 220 = 8

Something like

double value = a / b; // 2.283214570
double x = Math.Truncate(value * Math.Pow(10, decimalnumber)); // 228
double y = Math.Truncate(value * Math.Pow(10, decimalnumber - 1)) * 10; // 220
double digit = x - y; // 228 - 220 = 8;

Be careful about overflows etc.


Awesome. This code only needs slight modification to make it work though.

Math.Truncate = trunc in C
Math.Pow = pow in C

But yeah, this is irrelevant.

Thanks.

Edit: Actually, it seems I've forgotten to say it's for C, but yeah... it's obvious how to fix this.

Edit 2: You may also want to change 'digit' to: int digit = (int)x - (int)y; because it's more natural for this scenario.

That's a lot more complicated than it needs to be:


int(value * pow(10, decimalnumber)) % 10


2.283214570 * 100 => 228.3214570
cast to int => 228
228 mod 10 => 8

Edit: Forgot to include "value" in the above :-/

I like this solution, assuming that we don't have to solve cases where iee754 fails. For example on the edge case of the original question of 1/60000 = 1.66666666666666674653622670643E-5, you get a failure for most values of n > 20, because you lose the precision. So if the requirement includes handling a or b > 60000 and n > 20, americanumlaut's solution fails. (or if the requirement is a, b ~3 and n > 60,000, it also fails - weren't exactly clear on which is which).

If the requirement is only b ~ 60000 and n < 20, you're probably ok, but you might want to check the power multiplication edge cases, because you'll lose some precision there too.


I like del's solution better, and it's probably what the prof is going for in this case. If the integer requirements are only up to 60k, you won't have any precision problems if you roll your own long division, whereas a 64 bit double isn't sufficient.
Who after all is today speaking about the destruction of the Armenians?
Oaky
Profile Joined August 2012
United States95 Posts
February 14 2013 04:07 GMT
#4856
For you software engineers im curious, what is the common IQ of programmers? Are there a lot of idiots in the profession?

May seem like a blunt question but I'm going into IT and I have around 120 IQ; gotta wonder where that puts me.
SOOOOOOO MANY BANELINGS!
phar
Profile Joined August 2011
United States1080 Posts
Last Edited: 2013-02-14 04:15:01
February 14 2013 04:09 GMT
#4857
IQ is pretty meaningless for software engineering. Solving logic puzzles only gets you so far.

Also IT is as very general term that sometimes includes software engineering and sometimes does not.
Who after all is today speaking about the destruction of the Armenians?
Oaky
Profile Joined August 2012
United States95 Posts
Last Edited: 2013-02-14 04:17:36
February 14 2013 04:17 GMT
#4858
Yeah fair enough

For all you programmers~
SOOOOOOO MANY BANELINGS!
JeanLuc
Profile Joined September 2010
Canada377 Posts
February 14 2013 04:26 GMT
#4859
On February 14 2013 13:07 Oaky wrote:
For you software engineers im curious, what is the common IQ of programmers? Are there a lot of idiots in the profession?

May seem like a blunt question but I'm going into IT and I have around 120 IQ; gotta wonder where that puts me.


There are a lot of idiots in every walk of life. There are idiotic aspects to almost every personality. The question of paramount importance is: does it blend?

If you can't find it within yourself to stand up and tell the truth-- you don't deserve to wear that uniform
Oaky
Profile Joined August 2012
United States95 Posts
February 14 2013 04:32 GMT
#4860
On February 14 2013 13:26 JeanLuc wrote:
Show nested quote +
On February 14 2013 13:07 Oaky wrote:
For you software engineers im curious, what is the common IQ of programmers? Are there a lot of idiots in the profession?

May seem like a blunt question but I'm going into IT and I have around 120 IQ; gotta wonder where that puts me.


There are a lot of idiots in every walk of life. There are idiotic aspects to almost every personality. The question of paramount importance is: does it blend?


You just killed me.
SOOOOOOO MANY BANELINGS!
Prev 1 241 242 243 244 245 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 7h 55m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko502
LamboSC2 323
Trikslyr41
Rex 25
Codebar 21
StarCraft: Brood War
Britney 26733
Calm 7919
Bisu 2798
Shuttle 1233
Larva 664
BeSt 544
Mini 420
Rush 396
firebathero 380
Soma 376
[ Show more ]
Stork 358
Snow 316
ZerO 305
EffOrt 288
Light 287
ggaemo 224
Leta 185
actioN 167
PianO 103
Zeus 81
Mind 78
Sea.KH 75
Sharp 70
Backho 66
sorry 43
HiyA 42
[sc1f]eonzerg 40
Barracks 40
Free 39
Movie 25
soO 19
Terrorterran 16
IntoTheRainbow 16
Shinee 15
Sacsri 12
ajuk12(nOOB) 8
ivOry 7
Dota 2
Gorgc7195
Counter-Strike
fl0m1163
edward111
oskar85
adren_tv57
Heroes of the Storm
MindelVK19
Other Games
singsing1968
B2W.Neo840
hiko813
FrodaN713
DeMusliM312
crisheroes160
Hui .158
Liquid`VortiX111
QueenE104
ArmadaUGS102
Sick101
Grubby43
ZerO(Twitch)19
Organizations
Dota 2
PGL Dota 2 - Main Stream39
StarCraft: Brood War
Kim Chul Min (afreeca) 8
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• poizon28 48
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• FirePhoenix0
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis3622
• TFBlade988
• Shiphtur111
Upcoming Events
PiGosaur Cup
7h 55m
Replay Cast
16h 55m
Afreeca Starleague
17h 55m
hero vs YSC
Larva vs Shine
Kung Fu Cup
18h 55m
Replay Cast
1d 7h
KCM Race Survival
1d 16h
The PondCast
1d 17h
WardiTV Team League
1d 19h
OSC
1d 19h
Replay Cast
2 days
[ Show More ]
WardiTV Team League
2 days
RSL Revival
3 days
Cure vs Zoun
herO vs Rogue
WardiTV Team League
3 days
Platinum Heroes Events
3 days
BSL
4 days
RSL Revival
4 days
ByuN vs Maru
MaxPax vs TriGGeR
WardiTV Team League
4 days
BSL
5 days
Replay Cast
5 days
Replay Cast
5 days
Afreeca Starleague
5 days
Light vs Calm
Royal vs Mind
Wardi Open
5 days
Monday Night Weeklies
5 days
OSC
6 days
Sparkling Tuna Cup
6 days
Afreeca Starleague
6 days
Rush vs PianO
Flash vs Speed
Liquipedia Results

Completed

Proleague 2026-03-23
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
BSL Season 22
CSL Elite League 2026
CSL Season 20: Qualifier 1
ASL Season 21
Acropolis #4 - TS6
RSL Revival: Season 4
Nations Cup 2026
NationLESS Cup
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

2026 Changsha Offline CUP
CSL Season 20: Qualifier 2
CSL 2026 SPRING (S20)
Acropolis #4
IPSL Spring 2026
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.