You are not logged in. Please login at www.codechef.com to post your questions!

×

Range overflow in VSN

When we are finding distance b/w P and Q (x2-x1)^2 + (y2-y1)^2 whole underoot.(for 2d case). Now according to constraint of co-ordinates (this value (x2-x1)^2 + (y2-y1)^2) can exceed max value possible to store)?

asked 12 Jun, 00:00

vaibhav026's gravatar image

3★vaibhav026
211
accept rate: 0%

converted to question 12 Jun, 15:29

1

Its guaranteed that co-ordinates of Q never exceed $2*{10}^{9}$ during entire time.

(12 Jun, 15:43) vijju123 ♦5★

As specified in the constraints that the absolute value of the coordinates can be at max 2 10^9 . Now if we take x2=2 10^9, x1=-2 10^9, y2=2 10^9, y1=-2 *10^9, so it's pretty clear the value

(x2-x1)^2 + (y2-y1)^2 turns out to be 32 *10^18 which does not fit into long type integers . So yes, it would overflow.

link

answered 12 Jun, 15:50

aman_robotics's gravatar image

5★aman_robotics
354
accept rate: 8%

edited 12 Jun, 15:52

Use long double.

(12 Jun, 15:52) vijju123 ♦5★

@vijju123 What about JAVA , it can support at max 64 bits

(12 Jun, 15:55) aman_robotics5★

Will double not suffice? Did you try that?

(12 Jun, 18:47) vijju123 ♦5★

@vijju123 no need to use long double, even for C++ double would suffice.

(12 Jun, 18:53) sorb19974★

yeah @vijju123 and @souradeep1999 you are right

(12 Jun, 19:04) aman_robotics5★

Thats true, but long double afaik is more precise than double. I never trust floating points so thought better safe than sorry xD

(12 Jun, 19:46) vijju123 ♦5★

How can long double store more than 64 bits i.e >10^18

(13 Jun, 10:12) vaibhav0263★
showing 5 of 7 show all

As that is 64 bit then i think it can also store same as c++ upto 2^64-1,thats nearly 18*10^18.

link

answered 12 Jun, 18:32

souradeep1999's gravatar image

5★souradeep1999
704
accept rate: 0%

long double or even double can be used. They have a huge range (around $1.7e^{308}$) (check here) even though they are 64 bits since they use floating point representation, i.e. have some bits reserved for exponent and some bits for mantissa.

link

answered 13 Jun, 16:45

sahil_bansal's gravatar image

3★sahil_bansal
0
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×288

question asked: 12 Jun, 00:00

question was seen: 464 times

last updated: 13 Jun, 16:45